Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL convert INT to datetime

Tags:

sql

datetime

int

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
like image 938
rubio Avatar asked May 22 '12 18:05

rubio


1 Answers

That's looking like an Unix-style epoch-based timestamp, i.e. 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.

like image 62
theglauber Avatar answered Sep 20 '22 06:09

theglauber