MySQL select formatted date from millisecond field

63,761

Solution 1

Try using the FROM_UNIXTIME function like this as given in the manual

SELECT FROM_UNIXTIME(1196440219);
 -> '2007-11-30 10:30:19'

You could also use formatting like this

mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),
    ->                      '%Y %D %M %h:%i:%s %x');
    -> '2007 30th November 10:30:59 2007'

Solution 2

If you want to get the microsecond from a field, use %f,

select FROM_UNIXTIME(DATE_COLUMN/1000,'%Y-%M-%d %H:%i:%s %f') from TABLE_NAME;

+-------------------------------------------------------+
| FROM_UNIXTIME(CREATETIME/1000,'%Y-%M-%d %H:%i:%s %f') |
+-------------------------------------------------------+
| 2016-March-18 16:02:54 342000                         |
+-------------------------------------------------------+

Source : MYSQL DATE_FORMAT

Share:
63,761
Jason C
Author by

Jason C

You may refer to me as any pronoun you wish.

Updated on April 03, 2020

Comments

  • Jason C
    Jason C about 4 years

    I have a column in a MySQL database that contains a date as milliseconds (epoch). I want to build an SQL query that formats the date as something human readable (day, month, year, hours, minutes, seconds in any format and time zone). Is there an SQL (or MySQL-specific) function to do this?