如何在T-SQL中将varchar转换为datetime? [英] How to convert varchar to datetime in T-SQL?
问题描述
我正在尝试将数据从 table1
填充到 table2
,它们的列数均相同。
I am trying to populate the data from table1
to table2
, both have the same number of columns.
表1
中的所有列的类型为 varchar
。 table2
中的列可以是 varchar
, int
或 datetime
等。
All the columns in table1
are of type varchar
. The columns in table2
could be varchar
, int
or datetime
, etc.
我的问题是在填充过程中如何进行转换?
My question is how to do the conversion during the populating?
这是我编写的示例查询。我错过了进行转换的部分。同样,我的 datetime
的格式为 mm / dd / yyyy hh:mm:ss
。
This is a sample query that I wrote. I miss the part to do the conversion. Also the format of my datetime
is mm/dd/yyyy hh:mm:ss
.
insert into table2
select s.acty_id, s.notes_datetime, s.notes_data
from table1 t right join table2 s
on (t.acty_id =s.acty_id and t.notes_datetime =s.notes_datetime)
where t.acty_id is null
推荐答案
您将使用 CAST()
或 CONVERT( )
在您的字段上:
You will use a CAST()
or CONVERT()
on your field:
Declare @dt varchar(20)
set @dt = '08-12-2012 10:15:10'
select convert(datetime, @dt, 101)
对于查询,您将执行以下操作:
For your query you would do the following:
insert into table2
select s.acty_id, s.notes_datetime, s.notes_data
from table1 t
right join table2 s
on t.acty_id =s.acty_id
and convert(datetime, t.notes_datetime, 101) = s.notes_datetime
where t.acty_id is null
这篇关于如何在T-SQL中将varchar转换为datetime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!