我正在尝试将数据从table1
填充到table2
,两者具有相同的列数。
table1
中的所有列均为 varchar
类型。 table2
中的列可以是 varchar
、int
或 datetime
等。
我的问题是如何在填充过程中进行转换?
这是我编写的示例查询。我错过了进行转换的部分。另外,我的日期时间
的格式是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()
or CONVERT()
在你的领域:
Declare @dt varchar(20)
set @dt = '08-12-2012 10:15:10'
select convert(datetime, @dt, 101)
对于您的查询,您将执行以下操作:
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
关于sql-server-2008 - 如何在 T-SQL 中将 varchar 转换为 datetime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12099059/