Not abe to declare as timestamp in postgreSQL - sql

select *
From #####
where username in ('akhil') and between (now() ::timestamp and now() - interval '1 day'::timestamp)
Getting error in this line .
Error - cannot cast type interval to timestamp without time zone .

You don't need the second cast. You need a column name. Something like this:
select *
from #####
where username in ('akhil') and
<some date column> >= now() - interval '1 day' and
<some date column> < now();

the error is in the second cast
now() - interval '1 day'::timestamp
is interprested as.
now() - (interval '1 day')::timestamp
when you actually mean
(now() - interval '1 day')::timestamp
or possibly you mean
now()::timestamp - interval '1 day'
both are valid, but the the result you'll get when daylight saving starts or stops is different (hint interval '1 day' is the same as interval '24 hours' in the first)
There's a strong possiblity that you should actually be using timestamp with time zone for the database column.

Related

Select data between dates and between times of day

I want to query rows for a given time range and also filter between given times of day.
Example I want to filter for times of day between '9.00 AM' and '10.00 PM' of every date within a given time range.
My table looks like this.
This is my sample code:
SELECT *
FROM public.energy
WHERE time >= date_trunc('month', NOW() - INTERVAL '1 MONTH') AT TIME ZONE 'Asia/Bangkok'
AND time < date_trunc('MINUTE', NOW()- INTERVAL '1 MONTH') AT TIME ZONE 'Asia/Bangkok'
AND name = 'SWU0001'
ORDER BY id DESC;
I already select data between dates that I want, but I want to filter for specific times.
SELECT *
FROM public.energy
WHERE name = 'SWU0001'
AND time >= date_trunc('month' , now() AT TIME ZONE 'Asia/Bangkok' - interval '1 month') AT TIME ZONE 'Asia/Bangkok' -- !
AND time < date_trunc('minute', now() AT TIME ZONE 'Asia/Bangkok' - interval '1 month') AT TIME ZONE 'Asia/Bangkok' -- !
AND (time AT TIME ZONE 'Asia/Bangkok')::time BETWEEN '09:00' AND '22:00' -- !!!
ORDER BY id DESC;
Don't call a timestamptz column "time". The name is misleading, and it's a basic type name.
Also, to work with local time of 'Asia/Bangkok' you need to get the local time for that time zone before applying date_trunc(), and cast the adjusted value back to timestamptz at the end. Basics:
Ignoring time zones altogether in Rails and PostgreSQL

trunc(add_months(sysdate,3),'Q')-1 equivalent expression in Postgres

can anybody convert this oracle expression trunc(add_months(sysdate,3),'Q')-1) to postgresql?
Basically this expression gives you the last day of the current quarter (provided that you remove the last closing parenthese, which otherwise is a syntax error).
In postgres, you could phrase this as:
date_trunc('quarter', current_date) + interval '3 months' - interval '1 day'
This generates a timestamp value, that you can cast if you want a date (this distinction does not exist in Oracle, where a date stores the time component as well).
The Postgres equivalent of your Oracle calculation can be seen below.
select date_trunc('quarter', current_date + interval '3 month') - interval '1 day'

Syntax error near "from" in amazon redshift query

I am working on queries in Amazon RedShift.
I have created in query in which I am getting syntax error but I am unable to know the issue behind it. The query looks OK to me.
Below is the query:
select (TIMESTAMP WITHOUT TIME ZONE 'epoch' + (_action_date::float / 1000000) * INTERVAL '1 second') as eta
where eta >= (SELECT NOW() - INTERVAL '1 DAY')
from trips;
Here, _action_date is a bigint integer so I am converting it into timestamp format also.
I am trying to extract those rows in which _action_date is greater than or equal to yesterday date.
The error I am getting:
Error running query: syntax error at or near "from" LINE 1: ...ta where eta >= (SELECT NOW() - INTERVAL '1 DAY') from trips... ^
Please help me in correcting it. Any help would be highly appreciated.
select (TIMESTAMP WITHOUT TIME ZONE 'epoch' + (_action_date::float / 1000000) *
INTERVAL '1 second') as eta
from trips
where eta >= (SELECT getdate() - INTERVAL '1 DAY');

psql generate_series method doesn't support concat inside

The following sql code worked as expected
generate_series('2018-06-29 00:00:00','2018-06-29 23:00:00', interval '1 hour')
but when I put concat method instead of first 2 parameters it's rise an error message
generate_series(concat('2018-06-29 00:00:00', '+05'), concat('2018-06-29 23:00:00', '+05'), interval '1 hour')
The error message
function generate_series(text, text, interval) does not exist
If you concat it becomes as text data type. Hence you cannot generate series.
Below query will produce desired result
No need to write "interval". Since start and end are timestamp postgresql understands 5h and 1h are 5hours and 1hour
select
generate_series(timestamp '2018-06-29 00:00:00' + '5h',
timestamp '2018-06-29 23:00:00' + '5h',
'1h')
That is because generate_series() does not operate on strings. Convert to the right data type:
select generate_series(concat('2018-06-29 00:00:00'::text, '+05'::text)::timestamp,
concat('2018-06-29 23:00:00'::text, '+05'::text)::timestamp,
interval '1 hour'
)
No need for concat() or string operations.
If you want to add 5 hours to the start and ending timestamp, then just add that:
generate_series(timestamp '2018-06-29 00:00:00' + interval '5 hour',
timestamp '2018-06-29 23:00:00' + interval '5 hour', interval '1 hour')

date_sub ok with mysql, ko with postgresql

This query which works with mySQL doesn't work with Postgresql:
select ... from ... where (id = ... and ( h > date_sub(now(), INTERVAL 30 MINUTE)))
The error is:
Query failed: ERREUR: erreur de syntaxe sur ou près de « 30 »
Any ideas ?
DATE_SUB is a MySQL function that does not exist in PostgreSQL.
You can (for example) either use;
NOW() - '30 MINUTES'::INTERVAL
...or...
NOW() - INTERVAL '30' MINUTE
...or...
NOW() - INTERVAL '30 MINUTES'
as a replacement.
An SQLfiddle with all 3 to test with.
An interval literal needs single quotes:
INTERVAL '30' MINUTE
And you can use regular "arithmetics":
and (h > current_timestamp - interval '30' minute)
Try using something like :
select ...
from ...
where id = ...
and h > now() - INTERVAL '30 MINUTE'