All,
I have a sql which is like below used in informatica lookup sql.
select * from table where load_dt = trunc(sysdate) when the time is 2 am to 00.00 am
when time is between nextday 00.30am to 2.00 am, the same sql should read as
select * from table where load_dt = trunc(sysdate-1)
how can i make these two sql in single sql?
is it possible?
thanks
I'm not sure I fully understand the question but I'm assuming that if the hour is less than 2 am you want to subtract 1 from sysdate otherwise keep current sysdate.
You said; 2 am to 00.00 am and 00.30am to 2.00 am... but I interpreted it as above.
Select *
from table
where load_Dt = trunc(sysdate - case
when TO_CHAR(SYSTIMESTAMP,'HH24') < 2 then 1 else 0 end)
or
Select *
from table
where load_Dt = trunc(sysdate - case
when EXTRACT(HOUR FROM SYSTIMESTAMP) < 2 then 1 else 0 end
Depending if Timezone offset is needed or not.
You can just subtract two hours before truncating the date:
select *
from table
where load_dt = trunc(sysdate - 2/24.0) ;
Oracle also supports - interval '2' hour, but I'm old-fashioned sometimes.
Related
I am looking to obtain all data in a table from yesterday in SQL Oracle.
This is simply enough using the WHERE clause, i.e,
SELECT *
FROM My_Data
WHERE TO_DATE(My_Data.Date,'YYYY-MM-DD') = TRUNC(SYSDATE)-1
However if I now need to add more logic where if the day of the query is a Monday (SYSDATE) then obtain data between Friday and Sunday.
Using a between statement is no issue, I'm just not sure if I can include in a where statement given I'm unable to use case statement here.
Thanks
SELECT
*
FROM
My_Data
WHERE
TO_DATE(My_Data.Date,'YYYY-MM-DD')
Between Case When To_Char(SYSDATE, 'DY') = 'MON' Then TRUNC(SYSDATE)-3 ELSE TRUNC(SYSDATE)-1 END
And TRUNC(SYSDATE)-1
You can use the Case expression in Where clause. Regards...
Don't use TO_DATE on a column that is already a date (and if it is a string then don't store dates as strings).
So you are not dependent on the date language session parameter, you can compare the date to the start of the ISO week (which is independent of language) and you can compare on a date range so that Oracle can use an index on your date column:
SELECT *
FROM My_Data
WHERE "DATE" < TRUNC(SYSDATE)
AND "DATE" >= CASE TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW')
WHEN 0 -- Monday
THEN TRUNC(SYSDATE) - 3
ELSE TRUNC(SYSDATE) - 1
END;
or:
SELECT *
FROM My_Data
WHERE "DATE" < TRUNC(SYSDATE)
AND ( ( TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW') = 0 AND "DATE" >= TRUNC(SYSDATE) - 3 )
OR "DATE" >= TRUNC(SYSDATE) - 1
);
I have an SQL query where I'm trying to select only records from the past two weeks of the present day i.e. using the 'created_on' column. Any ideas how that is done? I know I can't use specific dates because present day is always changing.
select un.id
, t.type_name as type
, un.content_id
, un.app_link
, un.notification_text
, t.id as type_id
, un.seen_yn,un.created_on
, to_char(un.created_on,'YYYY-MM-DD: HH24:MI') as timestamp
from app.user_notifications as un
left join ref.types as t on t.id = un.notification_type
where un.active_yn = true
and un.user_id = 1
and un.seen_yn = false order by un.created_on desc
You can compare the created_on column with an expression that calculates "two weeks before today":
un.created_on >= current_date - interval '2 weeks'
alternatively you can also subtract 14 days
un.created_on >= current_date - 14
Add yet another condition, e.g.
... and un.created_on >= trunc(sysdate) - 14
In Oracle, when you subtract number from a DATE datatype value, you subtract that many days so - subtracting 14 is two weeks back.
SQL> select sysdate right_now,
2 trunc(sysdate) today_midnight,
3 trunc(sysdate) - 14 two_weeks_back
4 from dual;
RIGHT_NOW TODAY_MIDNIGHT TWO_WEEKS_BACK
------------------- ------------------- -------------------
22.03.2022 07:12:54 22.03.2022 00:00:00 08.03.2022 00:00:00
SQL>
Alternatively, if you prefer interval (which actually says what you're doing), then
SQL> select trunc(sysdate) - interval '14' day from dual;
TRUNC(SYSDATE)-INTE
-------------------
08.03.2022 00:00:00
SQL>
I want to select data where the operation_date between '01-Jan-2016' and yesterday. I used code as follows:
select *
where operation_date between '01-Jan-2016' and sysdate-1
from TABLE
But sysdate returns both date and time. Therefore, the above output includes all the data between '01-Jan-2016' and say, 14 Nov-2017 09:50:51. I only want the data before today. How to convert current time to date without time? Thanks.
Seems that you are looking for trunc().
Example
SELECT sysdate - 1 AS current_Date_Time
,trunc(sysdate) - 1 AS CURRENT_DATE1
,trunc(sysdate - 1) AS CURRENT_DATE2
FROM dual
Result
CURRENT_DATE_TIME CURRENT_DATE1 CURRENT_DATE2
----------------------------------------------------------
13.11.2017 18:08:41 13.11.2017 00:00:00 13.11.2017 00:00:00
DEMO
So the correct query will be as below.
SELECT *
WHERE operation_date BETWEEN DATE '2016-01-01' -- ANSI Date Literal
AND trunc(sysdate - 1)
FROM TABLE
OR
SELECT *
WHERE operation_date BETWEEN DATE '2016-01-01' -- ANSI Date Literal
AND trunc(sysdate) - 1
FROM TABLE
I have the following example :
select * from my_table
where date between ('19-06-2014 00:00:00,000000000 EUROPE/BUCHAREST') and ('19-06-2014 23:59:59,000000000 EUROPE/BUCHAREST'
If I run this query it returns the correct values . What I need is something like this :
select * from my_table
where date between ('today - 2DAYS ') and ('today - 1DAY')
Do you have any idea how I can achieve this?
Try following solution:
--for one day results
select * from my_table where date between sysdate - 2 and sysdate - 1
--If you wish to start from the beginning of the day:
select * from my_table where date between trunc(sysdate) - 2 and trunc(sysdate) - 1
FYI - sysdate referes the current date and substrating value will be counted in terms of day. If you wish to substract 12 hrs, you should substract 0.5.
Need a query that lists all the dates of the past 12 months
say my current date is 10-21-2013. need to use sysdate to get the data
The result should look like
10/21/2013
...
10/01/2013
09/30/2013
...
09/01/2013
...
01/31/2013
...
01/01/2013
...
11/30/2012
...
11/01/2012
Please help me with this..
Thanks in advance.
AVG
Allowing for leap years and all, by using add_months to work out the date 12 months ago and thus how many rows to generate ...
select trunc(sysdate) - rownum + 1 the_date
from dual
connect by level <= (trunc(sysdate) - add_months(trunc(sysdate),-12))
You could do something like this:
select to_date('21-oct-2012','dd-mon-yyyy') + rownum -1
from all_objects
where rownum <=
to_date('21-oct-2013','dd-mon-yyyy') - to_date('21-oct-2012','dd-mon-yyyy')+1
of course, you could use parameters for the start and end date to make it more usable.
-or- using sysdate, like this:
select sysdate + interval '-1' year + rownum -1
from all_objects
where rownum <=
sysdate - (sysdate + interval '-1' year)
Try this
SELECT SYSDATE,
SYSDATE-1,
<continue>
SYSDATE-30,
<continue>
.........
<continue>
SYSDATE-364,
SYSDATE-365
FROM DUAL;