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
Related
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.
I would like to print this table (displaying only 4 rows for brevity):
Dates
Period
01-MAR-2022
61
02-MAR-2022
61
03-MAR-2022
61
30-APR-2022
61
So far I have:
SELECT CAST(TRUNC(date_trunc('month',CURRENT_DATE) + interval '-2 month') AS DATE) + (n || 'day')::INTERVAL AS Dates
, date_trunc('month',CURRENT_DATE) + interval '-2 month' + INTERVAL '2 month' - date_trunc('month',CURRENT_DATE) + interval '-2 month' AS Period
FROM generate_series(0,61) n
Please help with a better way of generating the period and also replacing the hard-coded 61 in generate_series(0,61).
Thanks!
What are you actually trying to accomplish, it is not clear nor specified. BTW your query is invalid. It appears you looking to list each data from first date of 2 months prior to the last date of 1 month prior and the total number of days in that range. The following would give the first date, and using date subtraction gives the number of days.
with full_range( first_dt, num_days) as
( select date_trunc ('month', (current_date - interval '2 months'))::date
, date_trunc ('month', (current_date - interval '1 day'))::date -
date_trunc ('month', (current_date - interval '2 months'))::date
)
select *
from full_range;
With that in hand you can use the num_days with generate series with the expression
select generate_series(0, num_days-1) from full_range
Finally combine the above arriving at: (see demo)
with full_range( first_dt, num_days) as
( select date_trunc ('month', (current_date - interval '2 months'))::date
, date_trunc ('month', (current_date - interval '1 day'))::date -
date_trunc ('month', (current_date - interval '2 months'))::date
)
select (first_dt + n*interval '1 day')::date, num_days
from full_range
cross join (select generate_series(0, num_days-1) from full_range) gn(n);
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'
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'
I have the table users.
Columns:
user_id - integer
user_date - integer(unix timestamp)
Some of rows have user_date and some have NULL.
I need to find all user_id's which user_date includes period between '2012-10-21' and '2012-10-24'
SELECT *
FROM users
WHERE user_date BETWEEN
EXTRACT(EPOCH FROM date '2012-10-21') AND
EXTRACT(EPOCH FROM date '2012-10-24' + interval '1 day');
If you don't want the end date to be included, remove the bit adding a day to that.
select *
from users
where
timestamp 'epoch' + user_date * interval '1 second' between '2012-10-21' and '2012-10-24'
and
user_date is not null