考虑以下查询(在MSSQL 2008中):
SELECT dateModified FROM SomeTable;
这将返回javascript格式的浮点值(自1970年以来的毫秒数):
dateModified
============
1301598290687
1071003581343
1311951478593
如何将其转换为select中的datetime2?
在这里使用@Mikeal Eriksson答案中的公式。
我会将float转换为biint,然后创建datetime:
select
DATEADD(MILLISECOND,
cast(dateModified as bigint) % 1000,
DATEADD(SECOND, cast(dateModified as bigint) / 1000, '19700101'))
from sometable
请参阅带有演示的SQL Fiddle
一些Oracle示例-将to_date()替换为equivalent:
SELECT (1301598290687/60/60/24/1000) as Days
, to_date('01-01-1970','dd-mm-yyyy') as start_date
, to_date('01-01-1970','dd-mm-yyyy')+(1301598290687/60/60/24/1000) as converted_date
FROM dual
/
DAYS START_DATE CONVERTED_DATE
---------------------------------------------------------
15064.7950310995 1/1/1970 3/31/2011 7:04:51 PM
创建双表:
CREATE TABLE DUAL
(
DUMMY VARCHAR(1)
)
GO
INSERT INTO DUAL (DUMMY)
VALUES ('X')
GO