Adding interval to current timestamp in PostgreSQL - postgresql

I want to add an interval of 23 hours to the current date and time in PostgreSQL. The query should look something like the following:
SELECT timestamp LOCALTIMESTAMP(0) + interval '23 hours';
For some reason, the preceding statement returns an error. What could be wrong here and why is it not working?

Remove TimeStamp keyword
SELECT LOCALTIMESTAMP(0) + interval '23 hours'
or use
SELECT now() + interval '23 hours'

Related

current_date in redshift exclude today's date when i am using with between command

I want to query data for last 30 days including today from redshift table. below is my query.
my date_column's type is 'timestamp without timezone'
select *
from mytable
WHERE date_column BETWEEN current_date - INTERVAL '30 day' AND current_date
order by date_column desc;
It gives the result for 30 days. But it doesn't include today's result.
I want to query for 30 days result including today's result also.
If it's a timestamp don't use between as it also compares the time part. Use a range query:
where date_column >= current_date - interval '30 day'
and date_column < current_date + interval '1 day'
Note that the upper bound is using < together with "tomorrow"
With Postgres this could be simplified to
where date_column >= current_date - 30
and date_column < current_date + 1
but Redshift isn't Postgres and I don't know if that would work there.

Add an interval day to a date from postgresql

I would like to add a day to a date from postgreSQL, so i tried this one :
SELECT * FROM t_ask a
WHERE ask_datesend is null
AND a.ask_date_reception >= ('2019-06-12' + INTERVAL '1 day');
But it doesn't work.
But if i try that i have an answer :
SELECT * FROM t_ask a
WHERE ask_datesend is null
AND a.ask_date_reception >= '2019-06-12'
Little precision in a.ask_date_reception the date is like
"2019-06-12 15:28:01.982"
Try an explicit conversion:
a.ask_date_reception >= ('2019-06-12'::date + INTERVAL '1 day');
Or use timestamp or timestamp with time zone instead of date.

add time interval with value from selected column in postgres

I am new in Postgres and want to know if there is a better way to solve time interval problem in Postgres.
In MySQL i have:
Select STR_TO_DATE(start_time - interval (tkc + tct) second + interval 3 hour, '%Y-%m-%d %H:%i:%s') as start_time
from table
For Postgres I found and wrote query as:
select start_time - (interval '1 second' * (tkc + tct)) + interval '3 hour'
from table
Thanks for all the answers. Wonderful to be part of community.
I was able to solve this as below.
select start_time - ((tkc + tct)|| ' seconds')::interval + interval '3 hour' as start_time from table

PostgreSQL - Difference between Current Date and a Date from Database

I'm trying to make a SELECT query which will compare current time with time at database. For example in database there is a record '2018-02-07 12:00:00' and I wanna compare it to current time. If current time is '2018-02-07 11:00:00', record '2018-02-07 12:00:00' should be visible in results. It should compare two dates and shows only those who are 1h before or after current time.'
Tried something like this:
SELECT * FROM events WHERE age(current_date, event_date) < '1 hour';
or
SELECT * FROM events WHERE event_date > (now() - INTERVAL '1 hour');
those that are 1h before or after current time
Wouldn't the logic look like this?
SELECT e.*
FROM events e
WHERE e.event_date > now() - INTERVAL '1 hour' AND
e.event_date < now() + INTERVAL '1 hour'

How to calculate end of the month in Postgres?

How to calculate end of the month in Postgres? I have table with column date datatype. I want to calculate end of the month of every date. For Eg. In the table there values like "2015-07-10 17:52:51","2015-05-30 11:30:19" then end of the month should be like 31 July 2015,31 May 2015.
Please guide me in this.
How about truncating to the beginning of this month, jumping forward one month, then back one day?
=# select (date_trunc('month', now()) + interval '1 month - 1 day')::date;
date
------------
2015-07-31
(1 row)
Change now() to your date variable, which must be a timestamp, per the docs. You can then manipulate this output (with strftime, etc.) to any format you need.
Source
SELECT TO_CHAR(
DATE_TRUNC('month', CURRENT_DATE)
+ INTERVAL '1 month'
- INTERVAL '1 day',
'YYYY-MM-DD HH-MM-SS'
) endOfTheMonth
Hi I tried like this and it worked
Date(to_char(date_trunc('month'::text, msm013.msa011) + '1 mon - 1 day '::interval , 'DD-MON-YYYY') )
Thanks a lot!!