0

我需要将 Long 值转换为日期时间格式。

例如,长值 - 20080506015600658 日期时间格式 - Tue May 06 01:56:00 IST 2008

4

1 回答 1

1

这是通过字符串操作实现它的丑陋方法:

declare @start bigint
set @start = 20080506015600658

select CONVERT(datetime,
    STUFF(STUFF(STUFF(STUFF(STUFF(STUFF(
        t,15,0,'.'),
          13,0,':'),
          11,0,':'),
           9,0,'T'),
           7,0,'-'),
           5,0,'-'))
from (select CONVERT(varchar(20),@start) as t) n

YYYY-MM-DD'T'hh:mm:ss.mil这基本上迫使它在进行转换之前符合模式。


这是用数学做的丑陋的罪恶方式:

declare @start bigint
set @start = 20080506015600658

select
    DATEADD(year,  (@start/10000000000000) - 1, --Because we already have 1 on starting date
    DATEADD(month, (@start/100000000000)%100 - 1, --Because we already have 1 on starting date
    DATEADD(day,   (@start/1000000000)%100 - 1, --Because we already have 1 on starting date
    DATEADD(hour,  (@start/10000000)%100,
    DATEADD(minute,(@start/100000)%100,
    DATEADD(second,(@start/1000)%100,
    DATEADD(millisecond,@start%1000,CONVERT(datetime2,'0001-01-01'))))))))
于 2013-07-10T06:33:41.560 回答