简体   繁体   English

SQL将INT转换为datetime

[英]SQL convert INT to datetime

I have 1323648000 which is int(10). 我有1323648000,这是int(10)。 I need to be able to convert it to a date format of some sort. 我需要能够将其转换为某种日期格式。 Something like dd/hh/yy hh:mm:ss. 像dd / hh / yy hh:mm:ss。 I have tried to us a few examples on here but i cannot seem to get it to work. 我在这里尝试了几个例子,但我似乎无法让它工作。 I have tried to cast it as a varchar(10) and convert but nothing. 我试图把它作为varchar(10)投射并转换但没有。 Excel also outputs ########. Excel还输出########。 Is the number incorrect then? 这个数字不正确吗?

SELECT
engine4_openid_statusstats.openidstat_id,
CONVERT(datetime,CAST(engine4_openid_statusstats.openidstat_time AS varchar(10),3),
engine4_openid_services.openidservice_id,
engine4_openid_services.openidservice_name
FROM
engine4_openid_statusstats ,
engine4_openid_services

That's looking like an Unix-style epoch-based timestamp, ie number of seconds since 1970. 这看起来像一个基于Unix风格的基于纪元的时间戳,即自1970年以来的秒数。

The conversion is RDBMS-specific. 转换是特定于RDBMS的。 With SQLITE you'd do 使用SQLITE,你可以做到

select datetime( 1323648000, 'unixepoch' );

and that yields 2011-12-12 00:00:00 (GMT). 并产生2011-12-12 00:00:00(格林威治标准时间)。

Check your RDBMS documentation for date-time conversion functions. 检查RDBMS文档以获取日期时转换功能。

如果确实存储为Epoch Time(它看起来像是),那么这些是自1970年1月1日以来的秒数。您可以通过将秒添加到此日期将其转换为日期时间。

SELECT DATEADD(SS, 1323648000, '01/01/1970')
TO_CHAR(column_name, 'DD/MM/YYYY hh:mm:ss')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM