我有两张桌子
temp
表中我有[infoDate] [nvarchar](255) NULL
info
表中我有[infoDate] [datetime] NULL
'17-JUL-14'
表
temp
等值
我想从info
表中插入temp
表。
insert into info (infoDate)
select (infoDate)
from temp
我尝试了以下查询,但无法将字符串转换为日期时间
select convert(varchar, '17-JUL-14', 105)
SELECT REPLACE(CONVERT(VARCHAR,'17-JUL-14',6),'-',' ')
SELECT SUBSTRING('17-JUL-14',1,2)+' '+SUBSTRING('17-JUL-14',4,3)+' '+SUBSTRING('17-JUL-14',8,2)
此查询有效
SELECT CONVERT(datetime,SUBSTRING('17-JUL-14',1,2)+' '+SUBSTRING('17-JUL-14',4,3)+' '+SUBSTRING('17-JUL-14',8,2),106)
但现在,如何插入info
表?
insert into info (infoDate)
select
(CONVERT(datetime, SUBSTRING(infoDate, 1, 2) + ' ' +
SUBSTRING(infoDate, 4, 3) + ' ' +
SUBSTRING(infoDate, 8, 2), 106))
from temp
答案 0 :(得分:1)
让我们简化代码。
<强>之前强>
insert into info (infoDate)
select
(CONVERT(datetime, SUBSTRING(infoDate, 1, 2) + ' ' +
SUBSTRING(infoDate, 4, 3) + ' ' +
SUBSTRING(infoDate, 8, 2), 106))
from temp
之后
insert into info (infoDate)
select
(CONVERT(datetime2, infoDate, 106))
from temp