简体   繁体   中英

SQL convert INT to datetime

I have 1323648000 which is 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. 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. Excel also outputs ########. 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.

The conversion is RDBMS-specific. With SQLITE you'd do

select datetime( 1323648000, 'unixepoch' );

and that yields 2011-12-12 00:00:00 (GMT).

Check your RDBMS documentation for date-time conversion functions.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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