Convert a timestamp to date in sql

March 5th, 2014

Most sql servers, including MySQL, are able to do some conversions and calculations for you. This is relevant if you want to perform a query directly on the sql server or if you want the conversion or calculation to be done in the sql server before it is returned to your script.

For example you can convert a unix timestamp to a readable date using this combination of functions:

SELECT id, title, body, FROM_UNIXTIME(created) AS created FROM articles WHERE id = 5;

In this query you select 3 columns and the result from a function from the articles table. The function FROM_UNIXTIME() takes a unix timestamp as parameter, which is what the column created contains.

Categories

IT

Tags