WHERE (internalagentname is not null or internalagentcode is not null)
and (source LIKE '%GETAWAY%' or source like '%VACATION%')
and initialbookingdate <= to_Date(to_char(sysdate-1,'MM/DD/YYYY'),
'MM/DD/YYYY')
and (ABS(total_revenue) + ABS(total_cost) + ABS(booking_adjustment))<>0
so, this is my final step query were I am pulling data from yesterdays date. unfortunately its reading this as sysdate - 1 (from current time) so that's why it has data from today current date as well, how can i change this so it only takes out data from 12 am and before? thanks
You should simply be using the logic:
initialbookingdate < trunc(sysdate - 1)
The problem is the <=. The current time has nothing to do with the issue, because there is no time component in the conversion back to a date. Nevertheless, your expression is way more complex than it needs to be.
Saying that you need data that belongs to "yesterday", have a look at this:
SQL> select
2 sysdate right_now,
3 trunc(sysdate) todays_midnight,
4 trunc(sysdate - 1) yesterdays_midnight
5 from dual;
RIGHT_NOW TODAYS_MIDNIGHT YESTERDAYS_MIDNIGHT
------------------- ------------------- -------------------
21.03.2018 21:31:46 21.03.2018 00:00:00 20.03.2018 00:00:00
SQL>
It means that one option is to select values whose initialbookingdate is between "yesterdays_midnignt" and "todays_midnight", i.e.
where initialbookingdate between trunc(sysdate - 1) and trunc(sysdate)
(note that BETWEEN is inclusive).
A simpler option, which would ruin index you might have on initialbookingdate (so - although simpler, use it with caution. On small data amount you wouldn't see problems, but when handling many rows - yes, you would) is
where trunc(initialbookingdate) = trunc(sysdate - 1)
try to use below code in your query condition,
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH:MI:SS AM';
SELECT TRUNC(sysdate)-(.00019/24) date_
FROM dual;
DATE_
----------------------
03/21/2018 11:59:59 PM
Related
I am looking at a query:
select JOB_ID from db where last_updated_date >= sysdate - 1/8
I went through the ORACLE SYSDATE documentation but couldn't understand what sysdate - 1/8 means.
Please clarify.
In Oracle, it means you're subtracting 1/8 of the whole day. As one day has 24 hours, its 1/8th part is 24/8 = 3 hours. So:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate col1,
2 sysdate - 1/8 col2
3 from dual;
COL1 COL2
------------------- -------------------
29.10.2021 22:18:36 29.10.2021 19:18:36
SQL>
date value remained the same (it is still today, 29.10.2021)
time has changed; right now, it is 22:18:36. When we subtract 3 hours from it, we get 19:18:36 (3 hours earlier)
It means that your query fetches rows whose last_updated_date column value is within the last 3 hours.
It will subtract 3 hours (24 hours/8) from sysdate, try running the following:
select to_char((sysdate - 1/8), 'mm/dd/yyyy hh24:mi:ss') from dual
This will give you 3 hours previous to the current date/time of the database.
I have a oracle sql code that is getting an inconsistent result when using sysdate compared to the manually entered dates (for last thursdays or fridays).
Ex:
Alter session set nls_date_format='DD-MON-RR';
query with the following where clause that runs custom dates on Mondays:
Manual entry that works correctly:
WHERE DATECOLUMN= to_date('2021/01/29','yyyy/mm/dd') -- key in previous Friday's date
WHERE DATECOLUMN= case when to_char(sysdate,'D')=2 then sysdate-3
else to_date('9999/01/01','yyyy/mm/dd')
end -- if today is monday, then pick last Friday's date for the run else pick a dummy date.
I am certain the alter session script in python is working correctly since I tested in Alteryx as well. However, I am still having trouble getting the result to match for manually entered dates. I tested the comparison of sysdate-3 vs. DATECOLUMN using select and it seems to be working on TOAD so I am not seeing the issue causing a mismatch.
What could be the cause of such a mismatch in results when both pieces of code are supposed to compare the DATECOLUMN with the same value?
The result of to_char(sysdate,'D') depends on current user sessions NLS_TERRITORY settings.
For more reliable solution better use for example:
TO_CHAR(sysdate, 'fmDay', 'NLS_DATE_LANGUAGE = American') = 'Monday'
SYSDATE - 3 returns both DATE and TIME components, e.g.
SQL> select sysdate - 3 from dual;
SYSDATE-3
-------------------
05.02.2021 11:06:04
SQL>
Your code suggests that DATECOLUMN contains only DATE component. Therefore, try with
trunc(sysdate) - 3
which will "remove" time component (actually, will set value to previous midnight):
SQL> select trunc(sysdate) - 3 from dual;
TRUNC(SYSDATE)-3
-------------------
05.02.2021 00:00:00
So, your code would then be
WHERE DATECOLUMN= case when to_char(sysdate,'D')=2 then trunc(sysdate) - 3
else to_date('9999/01/01','yyyy/mm/dd')
end
I'm new to oracle sql and I'm trying to run the following query but getting above mentioned error.
SELECT CONVERT(DATETIME,CASE WHEN DATEPART (Hour, OrderTime) < 5
THEN DATEADD(Day, -1, CONVERT(date, ordertime))
ELSE CONVERT(date, ordertime)
END) AS ItemOrderTradingDate FROM TBL_ITEM
Please help.
It seems that you want to extract "hour" from a date column. If that's so, see these two options:
SQL> select ordertime,
2 to_char(ordertime, 'hh24') hour_1,
3 extract(hour from cast(ordertime as timestamp)) hour_2
4 from tbl_item;
ORDERTIME HOUR_1 HOUR_2
------------------- -------- ----------
06.04.2020 08:01:05 08 8
SQL>
Error you got when running Ankint's query was because extract - for certain values (hours, minutes, seconds) works on timestamps, not dates.
I'm not familiar with functions you used (convert, datepart, dateadd) so I can't tell what you really want to do once you find which hour it is. dateadd looks like you'd want to add (or subtract) 1 day from some data value. If that's so, then just do it: date arithmetic is (by default) done by days anyway:
SQL> select sysdate right_now,
2 sysdate + 1 tomorrow,
3 sysdate - 1 yesterday
4 from dual;
RIGHT_NOW TOMORROW YESTERDAY
------------------- ------------------- -------------------
06.04.2020 08:05:34 07.04.2020 08:05:34 05.04.2020 08:05:34
SQL>
You may try below query -
SELECT CASE WHEN EXTRACT(HOUR FROM OrderTime) < 5
THEN TRUNC(ordertime) - 1
ELSE TRUNC(ordertime)
END AS ItemOrderTradingDate
FROM TBL_ITEM
I have a table that I need to filter based on date. For my problem, I have to filter the record which are relevant only for the current date without the time portion of the date.
For that I have used the following approach. Query seems to be working fine but I would like to know whether there are any pitfalls in this approach.Part of the WHERE clause related to the query is as follows.
AND TO_CHAR(EOD_DATE,'YYYY-MM-DD') IN TO_CHAR(sysdate,'YYYY-MM-DD')
EOD_DATE is the field in the table I take into consideration.I want records that EOD_DATE value is in the current date regardless of the time portion of EOD_DATE.
Thanks in advance :)
DATE datatype is up to a second precise. Therefore:
SQL> select trunc(sysdate) d_from, trunc(sysdate + 1) - 1/(24*60*60) d_to from dual;
D_FROM D_TO
------------------- -------------------
21.06.2018 00:00:00 21.06.2018 23:59:59
says that - if you want to be able to use index on the EOD_DATE column - you should consider
where eod_date between trunc(sysdate)
and trunc(sysdate + 1) - 1/(24*60*60)
Your solution works, but you are using a double conversion to compare strings instead of comparing dates. Besides, you don't need IN but an =
This could be a better, more readable way:
... and trunc(sysdate) = trunc(EOD_DATE)
Simply use TRUNC to remove the time component (or rather set it to 00:00:00) and then compare the dates:
WHERE TRUNC(EOD_DATE) = TRUNC(SYSDATE)
I am trying to find records from yesterdays 10:30 PM till today's 10:30 PM with SQL query. Please help me with sql query to find such records.
Maybe its a duplicate question, if so please link me to that. Don't want any pl-sql function.
A simple way to do this is to subtract times and compare dates. So, one way is:
select t.*
from t
where trunc(datecol) = trunc(sysdate - 1.5/24);
It is more efficient to use a direct comparison (because Oracle can more readily use an index):
select t.*
from t
where datecol >= trunc(sysdate) - 1.5/24 and
datecol < trunc(sysdate) + 1 - 1.5/24;
Note: You can also use interval for this purpose, if you are less old-fashioned than I am:
select t.*
from t
where datecol >= trunc(sysdate) - interval '90' minute
datecol < trunc(sysdate) + interval '1' day - interval '90' minute;
You can get the yesterday date with SYSDATE - 1. You would need something like this:
SELECT ...
FROM ...
WHERE date_field BETWEEN SYSDATE-1 AND SYSDATE