im trying to get a query working that will show the last 90 days of data. this is my timestamp code(it was a unix timestamp) :
"" to_date('1970-01-01','YYYY-MM-DD') +
numtodsinterval(c.f_crtm,'SECOND')- 6/24 as "oracle date" ""
this is the format of my date when the above code is run: 21-JUN-2020 15:48:59
i know it has something to do with the SYSDATE -90 but i cant figure it out.
any help would be great.
I am thinking of something like this:
(date '1970-01-01' + c.f_crtm * interval '1' second) - interval '6' hour > sysdate - interval '90' day
Or to use an index on (f_crtm), you can rearrange the logic:
c.f_crtm > (trunc(sysdate - interval '90' day + interval '6 hour') - date '1970-01-01')*24*60*60
Related
Could you please help me as below statement , i want to migrate from Greenplum to HiveSQL. kindly help me.
(date_trunc('week',idate) - INTERVAL '1 week')::DATE date_from
((date_trunc('week',idate) - INTERVAL '1 week')::DATE + '6 days'::INTERVAL)::DATE
date_trunc('week',idate)::DATE
note: idate is i have to parse the argument like 2021-02-20
If you are looking to
find start of week then use select next_day(date_sub(current_date, 7), 'MON')
add 1 week to the date then use select current_date + interval 7 day
convert date to string then use select to_date(current_date )
Now, from your code, it seems, you are looking for start of week and then deducting 1 week from that.
(date_trunc('week',idate) - INTERVAL '1 week')
This can be re-written in hive like below.
next_day(date_sub(current_date, 7), 'MON') - interval 7 day
I assumed Monday is your start of week. Please validate the SQL before using it.
I want to Increment/Decrement the Year , Day , Month by pulling in the Current Date in Snowflake in a single query ?
For e.g.
Suppose Current System Date - 04082021 I want to make it 05092022. I have tried the dateadd function but I suppose it allows only one part i.e. either year or month or day to be incremented at once.
Is it Possible ? If Not, What are the other alternatives ?
If you want to useDATEADD function, here is the code:
SELECT DATEADD(YEAR,+1,DATEADD(MONTH,+1,DATEADD(DAY,+1,CURRENT_DATE())));
returns 2022-09-05 from 2021-08-04
If you want to keep your format, Please use following code
SELECT TO_CHAR(DATEADD(YEAR,+1,DATEADD(MONTH,+1,DATEADD(DAY,+1,CURRENT_DATE()))),'DDMMYYYY');
I would do the arithmetic in thee parts:
select current_date + interval '1 year' + interval '1 month' + interval '1 day'
You can also use:
select current_date + interval '1 year, 1 month, 1 day'
My goal is to have the query grab data from 2 days ago. I don't want to have to keep inputting the date like this:
WHERE usage_start_date
BETWEEN TIMESTAMP '2020-09-09 00:00:00.000' and TIMESTAMP '2020-09-09
23:59:59.999'
but instead something like:
usage_start_date = current_date - interval '2' day
the above works for my Athena Presto SQL query, but for some reason will not give all the data that ran in those 24 hours, instead giving about half the day. Is there a way to do a statement like this one to ensure it gives ALL data in that day?
WHERE current_date - interval '2' day AND
BETWEEN TIMESTAMP '00:00:00.000' and TIMESTAMP '23:59:59.999'
without inputting the year, month, day? It seems like TIMESTAMP needs the y/m/d but what about doing a LIKE so it picks up the hour, minute, second but no need to put the y/m/d?
To get a timestamp for the start of the day that was two days ago you can do
DATE_TRUNC('day', NOW() - INTERVAL '2' DAY)
e.g.
WHERE usage_start_date >= DATE_TRUNC('day', NOW() - INTERVAL '2' DAY)
AND usage_start_date < DATE_TRUNC('day', NOW() - INTERVAL '1' DAY)
You can use below query to achieve the task by fetching the hour and date from the usage_start_date
select * from table where hour(usage_start_date) between 0 and 23 and current_date - interval '2' day = date(usage_start_date)
I would suggest:
WHERE usage_start_date >= CURRENT_DATE - INTERVAL '2' DAY AND
usage_start_date < CURRENT_DATE - INTERVAL '1' DAY
Is it possible to do something like this?
SELECT
TO_CHAR(SYSDATE-1, 'MM-DD-YYYY 08:00:00') "Yesterday",
I'd like to get the sysdate from yesterday at 8am
You would write this as:
trunc(sysdate - 1) + interval '8' hour
Or:
trunc(sysdate) - interval '16' hour
Or you can do date arithmetics with integer values rather than intervals:
trunc(sysdate) - 16/24
Truncate the sysdate. That will remove the time portion of the date.
Then add 8/24 (8 hours of the 24), 8/24 equals to 1/3 - 8 hours is a third of a day.
Then display this date value in the format DD-MON-YYYY HH24:MI:SS.
This all adds up to:
SELECT TO_CHAR(TRUNC(SYSDATE) - 1 + 1/3,'DD-MON-YYYY HH24:MI:SS') FROM DUAL;
I am writing this sql query but it says now non-numeric character was found where a numeric was expected.Where is the mistake??
SELECT dr.*
from daily_data_reading dr
where pod_id='01611152005960'
and DAILY_READING_DATE BETWEEN to_date(' min(daily_reading_date)','DD-MM-YY HH24:MI:SS')+ interval '1' day
AND to_date('trunc(SYSDATE) 23:59:59','DD-MM-YY HH24:MI:SS') + interval '1' day;
You are using to_date() in the wrong way
It makes no sense to call to_date() on a date value to convert that date value to a date.
Also to_date() expects a string formatted as a valid date.
Neither 'min(daily_reading_date)' nor 'trunc(SYSDATE) 23:59:59' is a valid date string.
So
to_date('min(daily_reading_date)','DD-MM-YY HH24:MI:SS')+ interval '1' day
needs to be:
min(daily_reading_date) + interval '1' day
And
to_date('trunc(SYSDATE) 23:59:59','DD-MM-YY HH24:MI:SS') + interval '1' day
should be:
AND trunc(SYSDATE) + interval '1' day
So your complete query should be:
SELECT dr.*
from daily_data_reading dr
where pod_id = '01611152005960'
and DAILY_READING_DATE BETWEEN min(daily_reading_date) + interval '1' day
AND trunc(SYSDATE) + interval '1' day;
But the above query will just trigger the next error: you can't use min(daily_reading_date) without applying a group by clause.
But you should ask a new question for that - including sample data and table definitions.