Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: How do I format an int timestamp as readable date string?

Tags:

postgresql

Say I have a column that contains unix timestamps - an int representing the number of seconds since the epoch. They look like this: 1347085827. How do I format this as a human-readable date string in my SELECT query?

like image 329
culix Avatar asked Sep 08 '12 07:09

culix


People also ask

What is the format of timestamp in PostgreSQL?

Postgres DATE data type Postgres uses the DATE data type for storing different dates in YYYY-MM-DD format. It uses 4 bytes for storing a date value in a column. You can design a Postgres table with a DATE column and use the keyword DEFAULT CURRENT_DATE to use the current system date as the default value in this column.

How do I convert a date to a string in PostgreSQL?

Postgresql date to string yyymmdd In Postgresql, dates can be converted in specific string formats like yyymmdd, where yyyy for a year, mm for a month, and dd for the date. For that conversion, we will use the to_char function to format the date as a string. Syntax: TO_CHAR(date_value, string_format);


1 Answers

Postgresql has a handy built-in function for this: to_timestamp(). Just wrap that function around the column you want:

Select a, b, to_timestamp(date_int) FROM t_tablename
like image 181
culix Avatar answered Oct 06 '22 04:10

culix