PGSQL - How do I Add 5 hours to DATE in WHERE Clause? - postgresql

I either need to add 5 hours or convert from GMT to EST. The return is currently showing everything from 7p and later yesterday...
WHERE
incident.initial_symptom = 'Chrome Upgrade' AND
DATE(incident.install_completed) = CURRENT_DATE;

Instead of manually adding interval to get wanted time zone, use at time zone, eg:
t=# select now(), now() at time zone 'est';
now | timezone
------------------------------+---------------------------
2017-04-07 07:07:39.17234+00 | 2017-04-07 02:07:39.17234
(1 row)
Depending on your timezone, exactly same statement adding interval to your date gives different result, eg at DST shift hour:
t=# set timezone TO 'WET';
SET
t=# select '2017-03-26 00:00:00'::timestamptz + '1 hour'::interval;
?column?
------------------------
2017-03-26 02:00:00+01
(1 row)
t=# set timezone TO 'EET';
SET
t=# select '2017-03-26 00:00:00'::timestamptz + '1 hour'::interval;
?column?
------------------------
2017-03-26 01:00:00+02
(1 row)

Related

FROM_TZ equivalent in PostgreSQL

How can I convert this to work in PostgreSQL?
TO_CHAR(CAST(FROM_TZ(CAST(columnname AS TIMESTAMP), 'GMT') AT TIME ZONE 'US/Eastern' AS DATE),'MM/DD/YY HH:MI AM') AS dt
testdb=# select TO_CHAR(CAST('2020-02-28T18:43' AS TIMESTAMP) AT TIME ZONE 'UTC' AT TIME ZONE 'US/Eastern','MM/DD/YY HH:MI AM') as dt;
dt
-------------------
02/28/20 01:43 PM
(1 row)
To make it clear what's going on, we'll start with the cast to TIMESTAMP, show that adding the first AT TIME ZONE makes it a tz-aware timestamp, and then how the 2nd does the timezone conversion.
testdb=# select CAST('2020-02-28T18:43' AS TIMESTAMP),
testdb-# CAST('2020-02-28T18:43' AS TIMESTAMP) AT TIME ZONE 'GMT',
testdb-# CAST('2020-02-28T18:43' AS TIMESTAMP) AT TIME ZONE 'GMT' AT TIME ZONE 'US/Eastern';
timestamp | timezone | timezone
---------------------+------------------------+---------------------
2020-02-28 18:43:00 | 2020-02-28 18:43:00+00 | 2020-02-28 13:43:00
(1 row)
See the timezone conversion docs for more details.

postgresql - NOW() but less specific

When I use SELECT NOW(); I'll get an output like this:
now
-------------------------------
2019-09-09 18:55:38.794006-05
(1 row)
I want it like this:
now
-------------------
2019-09-09 18:55:38
(1 row)
How do I make NOW() round up/down accordingly? I tried SELECT NOW()::timestamptz(0); but it keeps adding -05 to the end of the time :(
You can to_char
select to_char(now(), 'YYYY-MM-DD HH:MI:SS')
or use date_truc to retain datatype timestamp with time zone
select date_trunc('minute', now());
Convert NOW() to a timestamp without timezone:
SELECT NOW()::timestamp(0)

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

postgresql error : Multiple decimal points

So I'm having this query:
SELECT
TO_CHAR(date_part('hour', created_at), 'YYYY-MM-DD HH24'),
to_char(created_at, 'day') ",
COUNT(*) AS "
FROM table
GROUP BY 1,2
ORDER BY 1 DESC
When I execute the query I get this:
ERROR: multiple decimal points
Searching stackoverflow I found some recommendations here:
How to format bigint field into a date in Postgresql? but I don't get why do I have to divide by 1000 and how this would apply in the case of the date_part function.
I assume created_at is a timestamp?.. I'm choosing from date_part(text, timestamp) and date_part(text, interval), if so date_part will return a double precision, to which you try to apply the mask 'YYYY-MM-DD HH24', eg:
v=# select date_part('hour', now());
date_part
-----------
9
and I don't see how you could possibly get year, month, day and hour from nine...
Yet I assume you wanted to apply the mask against truncated date to the hour precision, which is done with date_trunc(text, timestamp):
v=# select date_trunc('hour', now());
date_trunc
------------------------
2017-06-20 09:00:00+01
(1 row)
so now you can apply the time format:
v=# select to_char(date_trunc('hour', now()),'YYYY-MM-DD HH24');
to_char
---------------
2017-06-20 09
(1 row)
but if this is what you want, then you don't need to truncate time at all:
v=# select to_char(now(),'YYYY-MM-DD HH24');
to_char
---------------
2017-06-20 09
(1 row)
https://www.postgresql.org/docs/current/static/functions-datetime.html

Postgresql: add 24 hours on a timestamp

Hi i have to add 24 hours on a timestamp converted from a string in postgres db.
here my code:
select to_timestamp(timestamp_start, 'YYYY-MM-DD HH24:MI:SS.US') + interval '24 hour' as tstamp from tablename
the query works but it adds two 0 at the end of the timestamp: "2017-05-23 17:35:13.105867+00"
why and how to solve it?
+00 meant it is timestamp with timezone and your client timezone is UTC.
If you dont want those +00 on the screen, cast it to timestamp without timezone, eg:
t=# select now();
now
-------------------------------
2017-05-23 09:04:46.105322+00
(1 row)
Time: 0.690 ms
t=# select now()::timestamp;
now
----------------------------
2017-05-23 09:04:51.849522
(1 row)
Time: 0.537 ms
So for query in original post it would be:
select (to_timestamp(timestamp_start, 'YYYY-MM-DD HH24:MI:SS.US') + interval '24 hour')::timestamp as tstamp
from tablename