表IFCD结构如下:
tdate ttime updatemillisec
20140730 093014 500
...
如何将列合并到一个名为tdatetime的列,如yyyy-mm-dd hh:mm:ss.mmm格式?
我的回答:
SELECT CAST(LEFT(tdate, 4)+'-'+substring(tdate, 5, 2)+'-'+RIGHT(tdate, 2)+' '+
LEFT(ttime, 2)+':'+substring(ttime, 3, 2)+':'+RIGHT(ttime, 2)+'.'+
CAST(updatemillisec AS varchar) AS DateTime)
答案 0 :(得分:1)
DECLARE @T TABLE (tdate VARCHAR(10)
,ttime VARCHAR(10)
,Updatemillisec VARCHAR(10))
INSERT INTO @T
VALUES ('20140730','093014','500')
SELECT CONVERT(VARCHAR(25), LEFT(tdate, 4) + '-' +
SUBSTRING(tdate, 5, 2) + '-' +
RIGHT(tdate, 2) + SPACE(1) +
LEFT(ttime, 2) + ':' +
SUBSTRING(ttime, 2, 2) + ':' +
RIGHT(ttime, 2) + '.' +
Updatemillisec, 109)
FROM @T
答案 1 :(得分:0)
请试试这个。您必须连接时间并更新millisec并使其达到此格式hh:mm:ss.MMM
。一旦采用该格式,以YYYYMMDD hh:mm:ss.MMM
DECLARE @Table TABLE (tdate VARCHAR(10),
ttime VARCHAR(10),
updatemillisec VARCHAR(10))
INSERT INTO @Table
VALUES('20140730', '093014', '500')
SELECT CAST(
( tdate + ' '
+ LEFT(ttime, 2) + ':'
+ SUBSTRING(ttime, 3, 2) + ':'
+ RIGHT(ttime, 2) + '.'
+ updatemillisec) AS DATETIME
) AS tDateTime
FROM @Table