Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date format into milliseconds in postgresql?

Tags:

postgresql

I have date in postgresql in format "17/12/2011".

How can i convert it into milliseconds using select clause of postgreql ?

Currently i am just executing select clause as

select tableDate,tableSales 
from table_name

I want to have something like when I select tableDate it should be converted into milliseconds using some postgresql functions.

tableDate DATE
tableSales Numeric
like image 821
Nidhi Avatar asked Mar 08 '16 07:03

Nidhi


People also ask

How do I change the date format 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.

What is the date format in PostgreSQL?

PostgreSQL uses the yyyy-mm-dd format for storing and inserting date values. If you create a table that has a DATE column and you want to use the current date as the default value for the column, you can use the CURRENT_DATE after the DEFAULT keyword. Let's look into some examples for better understanding.

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

extract(epoch from ...) will return the number of seconds since 1970-01-01 00:00:00 so all you need to do is to multiply that by 1000:

select extract(epoch from tableDate) * 1000, tableSales 
from table_name

More details in the manual:

http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

like image 136
a_horse_with_no_name Avatar answered Oct 19 '22 21:10

a_horse_with_no_name