Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert unix timestamp column to date format in PostgreSQL?

Tags:

postgresql

I'm trying to obtain the results in yyyy-mm-dd format from unixtimestamp column but getting in yyyy-mm-dd hh:mm:ss

My data in receive_time column is following:

recieve_time

1557866863  |
1557866863  |
1557866863  |
1557866863  |
1557866864  |
1557866864  |
1557866864  |
1557866864  |

Following is my query:

SELECT
to_timestamp(recieve_time) as date, count(*)
 FROM public.cdrs WHERE 
usage_type='0800 Voice Incoming' 
and to_timestamp(recieve_time) >='2019-05-01 00:00:00'
AND to_timestamp(recieve_time) <'2019-06-01 00:00:00'
AND main_bzd >0 
group by to_timestamp(recieve_time)

Getting this:

date                 |count|

-------------------|-----|

2019-05-01 00:00:2 |1    |
2019-05-01 00:03:2 |1    |
2019-05-01 01:20:0 |1    |
2019-05-01 01:21:1 |1    |
2019-05-01 01:53:0 |1    |
2019-05-01 02:16:5 |1    |
2019-05-01 02:33:5 |1    |
2019-05-01 02:39:4 |1    |
2019-05-01 02:55:3 |1    |
2019-05-01 03:32:5 |1    |
2019-05-01 03:35:0 |1    |

My requirement is following:

date        |count|

------------|-----|

2019-05-01  |19   |
2019-05-02  |15   |
2019-05-03  |17   |
like image 834
Baljot Singh Avatar asked Jun 14 '19 07:06

Baljot Singh


People also ask

How do I convert datetime to date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .

How do I convert timestamp to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

How do I change the date format of a column in PostgreSQL?

You can change the format in the postgresql. conf file. The date/time styles can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql. conf configuration file, or the PGDATESTYLE environment variable on the server or client.

How do I format date in PostgreSQL?

To get the current date and time, you use the built-in NOW() function. However, to get the date part only (without the time part), you use the double colons (::) to cast a DATETIME value to a DATE value. The result is in the format: yyyy-mm-dd .


2 Answers

Cast the result to date, both in the SELECT list and the GROUP BY clause:

CAST(to_timestamp(recieve_time) AS date)
like image 171
Laurenz Albe Avatar answered Sep 25 '22 08:09

Laurenz Albe


I think the simplest way to convert a unix timestamp to date format in PostgreSQL is below:

select to_timestamp(1557866863)::date;
 to_timestamp 
--------------
 2019-05-15
(1 row)

So your complete SQL would be:

select
     to_timestamp(recieve_time)::date as date,
     count(*)
from 
    public.cdrs 
where 
    usage_type='0800 Voice Incoming'
    and receive_time >= extract(epoch from cast('2019-05-01 00:00:00' as timestamptz))
    and receive_time <  extract(epoch from cast('2019-06-01 00:00:00' as timestamptz))
    and main_bzd >0 
group by 
    to_timestamp(recieve_time)::date

Notice: if there is a index created on your receive_time column, you'd better do not use the function on receive_time when it located at the where clause to filter rows, it will lead to fail to use index when execute SQL, the way above in my SQL is a better way. Good luck !

like image 44
Shawn.X Avatar answered Sep 24 '22 08:09

Shawn.X