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.
Related
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
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.
SELECT ORDER_NUM, CUSTOMER_NUM, CUSTOMER_NAME, ADD_DAYS (ORDER_DATE, 20)
FROM CUSTOMER, ORDERS;
Oracle Express says ADD_DAYS invalid? Any ideas what Am I doing wrong?
If you want to add N days to your days. You can use the plus operator as follows -
SELECT ( SYSDATE + N ) FROM DUAL;
You can use the plus operator to add days to a date.
order_date + 20
In a more general way you can use "INTERVAL". Here some examples:
1) add a day
select sysdate + INTERVAL '1' DAY from dual;
2) add 20 days
select sysdate + INTERVAL '20' DAY from dual;
2) add some minutes
select sysdate + INTERVAL '15' MINUTE from dual;
Some disadvantage of "INTERVAL '1' DAY" is that bind variables cannot be used for the number of days added. Instead, numtodsinterval can be used, like in this small example:
select trunc(sysdate) + numtodsinterval(:x, 'day') tag
from dual
See also: NUMTODSINTERVAL in Oracle Database Online Documentation
It's Simple.You can use
select (sysdate+2) as new_date from dual;
This will add two days from current date.
One thing about
select (sysdate+3) from dual
is that the sysdate is interpreted as date.
But if you want to use a custom date, not local, you need to make sure it is interpreted as date, not string. Like so (adding 3 days):
select (to_date('01/01/2020')+3) from dual
How do I select rows in the past starting from yesterday in Oracle DB where a field like created_date is a timestamp(6)?
I don't want to compare time, just date.
If you want exactly one day prior to the current time:
select *
from table t
where created_date < sysdate - 1;
If you want times before today:
select *
from table t
where created_date <= trunc(sysdate);
From the Oracle documentation on SELECT :
SELECT * FROM orders
WHERE created_date < TO_DATE('2014-04-28', 'YYYY-MM-DD');
I can pass this date format from my application, worked like a charm.
As you want to compare just date:
select *
from table t
where date(created_date) < DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
you can use cast function to deal with timestamp as date:
SELECT cast(SYSTIMESTAMP(6) as date)
FROM dual;
so you can select rows with "yesterdate" date by:
select ....
where cast(SYSTIMESTAMP(6) as date) like sysdate - 1
note: replace SYSTIMESTAMP(6) with column name which has timestamp type.
I need to select recods from oracle table for the current calendar week based on a date datatype field. Currently I am doing like this:
select * from my_table where enter_date > sysdate -7
If today is Thursday, this query will select records seven days from today which infiltrates to last week on Thursday. Is there a way to select records for the current calendar week dynamically?
If your week starts on a Monday then:
select ...
from ...
where dates >= trunc(sysdate,'IW')
For alternative definitions of the first day of the week, trunc(sysdate,'W') truncates to the day of the week with which the current month began, and trunc(sysdate,'WW') truncates to the day of the week with which the current year began.
See other available truncations here: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions255.htm#i1002084
to_char(sysdate, 'd') returns day of week in [1..7] range; so try using
select *
from my_table
where enter_date >= trunc(sysdate) - to_char(sysdate, 'd') + 1
Here is the SQLFiddel Demo
Below is the query which you can try
select Table1.*
from Table1
where dates > sysdate - TO_CHAR(SYSDATE,'D')