How to convert date format into milliseconds in postgresql? - 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

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

Related

Oracle to PostgreSQL round and trunc on date columns

Can you please help me to convert the below Oracle query to PostgreSQL query?. Looks like "round" and "trunc" functions are not there in postgreSQL.
Here "intime" is the date and time column i.e date datatype.
select round(((intime - trunc(CURRENT_DATE-1, 'DDD')) * 24),2) from tpe
Below is the posgreSQL format and o/p -2496 days -132:00:00
SELECT (('2021-02-19 08:00:00'::timestamp AT time zone 'EST' - CURRENT_DATE - (interval '1 day'))*24)
Below is ORACLE format and o/p -2464
select ((to_date('02/19/2021 8:00:00','MM/DD/YYYY HH24:MI:SS') - trunc(CURRENT_DATE-1, 'DDD')) * 24) from dual;
And When apply round function in postgreSQL it's throwing error
SELECT round((('2021-02-19 08:00:00'::timestamp AT time zone 'EST' - CURRENT_DATE - (interval '1 day'))*24),2)
"ERROR: function round(interval, integer) does not exist.
Idea is to get same output of oracle's one.

Postgresql timestamp difference greater than 1 hour

Hi I have a entrytime and exittime timestamp in my database, how can I query it to display only ones where the person exited more than an hour later;
Select * from store where EXTRACT(EPOCH FROM (exittime - entrytime))/3600 >60
That's what I have so far but it won't work, any help would be appreciated.
Just subtract the values and compare it with an interval
Select *
from store
where exittime - entrytime > interval '1 hour';
This assumes that both columns are defined as timestamptz or timestamp

Hive queries with dates

I have a little problem, I would like to filter a date with hive query but the output is empty. My column is string type
I tried this :
select * from my_table
where to_date(date) < to_date('01/08/19 00:00:00')
The format of my column date is 01/08/19 18:00:00
Dates in this format '01/08/19 00:00:00' are not in comparable format because in such format '02/08/19 00:00:00' is greater than '01/08/20 00:00:00'.
Use unix_timestamp and from_unixtime to convert to the comparable format ('yyyy-MM-dd HH:mm:ss'), then compare with date in the same format.
select * from my_table
where from_unixtime(unix_timestamp(date,'dd/MM/yy HH:mm:ss'),'yyyy-MM-dd HH:mm:ss') < '2019-08-01 00:00:00'

Convert Julian Date to Timestamp in PostgreSQL

I have a table full of "Julian Dates", that is the number of days and seconds away from 1/1/2035. I need to convert these to normal postgres timestamps. Can anyone help?
--Converts '2000-06-20 12:30:15' into an Epoch time base which gives a result of -12612.478993055556
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp - '2035-01-01 00:00:00'))/86400.00) as run_ts
--Question, how to convert -12612.478993055556 back into '2000-06-20 12:30:15'
select -12612.478993055556 ??? as run_ts
You can use to_timestamp() to convert an epoch to a timestamp.
The epoch you posted does not correspond to 2000-06-20, as you have removed another date 2035-01-01 from it.
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp )));
date_part
-----------
961504215
(1 row)
select to_timestamp(961504215);
to_timestamp
------------------------
2000-06-20 08:30:15-04
(1 row)
select to_timestamp(-12612.478993055556);
to_timestamp
-------------------------------
1969-12-31 15:29:47.521007-05
(1 row)
EDIT
Since you are not considering a true epoch but really a difference between two dates, you can simply add this difference to the reference date. You can use the day interval to remove the need to multiply by 86400 (seconds/day)
select '2035-01-01 00:00:00'::timestamp + interval '1' day * -12612.478993055556;
?column?
---------------------
2000-06-20 12:30:15

Postgres where clause compare timestamp

I have a table where column is of datatype timestamp
Which contains records multiple records for a day
I want to select all rows corresponding to day
How do I do it?
Assuming you actually mean timestamp because there is no datetime in Postgres
Cast the timestamp column to a date, that will remove the time part:
select *
from the_table
where the_timestamp_column::date = date '2015-07-15';
This will return all rows from July, 15th.
Note that the above will not use an index on the_timestamp_column. If performance is critical, you need to either create an index on that expression or use a range condition:
select *
from the_table
where the_timestamp_column >= timestamp '2015-07-15 00:00:00'
and the_timestamp_column < timestamp '2015-07-16 00:00:00';