[SQL]如何將字串yyyyMMddHHmmss轉成Datetime呢?
DECLARE @DateTimeVaue VARCHAR(30)
SET @DateTimeVaue = '20130402140837' -- 2013/04/02 14:08:37
--使用字串函數先將字串轉成日期格式字串再轉成DateTime (20130402140837 => 20130402 14:08:37)
SELECT CONVERT(DATETIME, LEFT(@DateTimeVaue, 8) + ' '
+ SUBSTRING(@DateTimeVaue, 9, 2) + ':'
+ SUBSTRING(@DateTimeVaue, 11, 2) + ':'
+ RIGHT(@DateTimeVaue, 2), 112)
--使用DATEADD來將時間加總
SELECT DATEADD(s, CAST(RIGHT(@DateTimeVaue, 2) AS INT),
DATEADD(n, CAST(SUBSTRING(@DateTimeVaue, 11, 2) AS INT),
DATEADD(hh,
CAST(SUBSTRING(@DateTimeVaue, 9, 2) AS INT),
CONVERT(DATETIME, LEFT(@DateTimeVaue, 8), 112))))
資料來源:http://www.dotblogs.com.tw/rainmaker/archive/2013/04/03/99823.aspx
沒有留言:
張貼留言