Oracle SQL Todays Date from Data - sql

Apologies as I am extremely green within SQL.
I am currently trying to run the below:
SELECT *
FROM test.message
WHERE MESSAGESUBJECT LIKE '%test%'
AND MESSAGEDATE = CURRENT_DATE
However the results are not pulling back - figured this to be the fact the format of the data within the field currently looks like this:
07-MAR-19 08.13.53.00000000 PM
So the obvious solution is to get it to look at the start of the field - where I'm losing track is that I need the query for a job that's running daily so I cant use exacts. Anyone able to assist?
Thanks,
D.

SELECT *
FROM test.message
where MESSAGESUBJECT like '%test%'
and MESSAGEDATE BETWEEN TRUNC(CURRENT_DATE) AND TRUNC(CURRENT_DATE) + 86399/86400
This will get rows where the message date was any time today.
The 86399/86400 bit is the number of seconds in a day minus 1, so it makes the BETWEEN range cover the whole day and none of the next day (which TRUNC(CURRENT_DATE)+1 would do).
Instead of adding 86399/86400, some find it clearer to add a whole day and then subtract out an an INTERVAL of 1 second. That also works, as does:
AND MESSAGEDATE >= TRUNC(SYSDATE)
AND MESSAGEDATE < TRUNC(SYSDATE)+1 -- Thanks #WernfriedDomscheit
Using
AND TRUNC(MESSAGEDATE) = TRUNC(CURRENT_DATE)
is logically correct, but it makes it impossible for Oracle to use an index on MESSAGEDATE if there is one, which can rob you of your best possible performance. But if you have a function based index on TRUNC(MESSAGEDATE), then that last way is also a reasonable option.

Related

SQL - Automatically adjust Where clause to previous month in YYYY-MM format

(This is all steps in containers within an Alteryx flow that is connecting to a Teradata source)
My SQL is incredibly rusty as it's been almost 8 years since I've needed to use it. I know this is a quite basic question. Basically I have several queries that need to be manually adjusted monthly to shift the month. in YYYY-MM format. They look like this:
Is the main one where I just adjust one backwards one month
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = '2023-01'
This is the secondary one where I adjust one backwards one month, and the others are same month or plus a month or more.
and A.B_MONTH in ('2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01','2023-05-01')
and B.Year_month = '2023-01'
How do I adjust the where clause to always be the needed relative references?
Any help is greatly appreciated
I tried using concat but it choked for some reason.
You can try this:
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = DATE_FORMAT( NOW() - INTERVAL 1 MONTH, '%Y/%m')
I don't understand your second need, but you can do it similar to my response.
Just play with the NOW() - INTERVAL X.
Pretty basic stuff. ADD_MONTHS to move your month around, TO_CHAR for your desired format.
To get the previous month:
select to_char(add_months(current_date,-1), 'YYYY-MM')

SQL SELECT date from table, and calculate how many days since that date

I'm looking to calculate how many days have passed since a specific date, retrieved from a table in my database. Based on the info I've found on W3Schools (Here), I have attempted using DATEDIFF, but am coming up against a couple of different errors I can't seem to work around.
I have included my code below, and based on this, what I want to happen is this: Select the "DD" from the "Wave_Data" table, and, based on "sysdate", work out how many days have lapsed since then.
SELECT DATEDIFF(WEEKDAY,:P1_DD,SYSDATE)
FROM WAVE_DATA
WHERE WAVE_NUMBER = :P1_WAVE;
The final calculation would then be inputted into a text field within my ApEx database.
Thank you in advance for any help you may be able to provide,
Dominic
In Oracle you can just subtract one Date from another to get the difference (in days) between them:
SELECT SYSDATE - :p1_dd
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
If you want to know the difference between the dates without any time parts then you can do:
SELECT TRUNC( SYSDATE ) - TRUNC( :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
or
SELECT FLOOR( SYSDATE - :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;

sqlalchemy select by date column only x newset days

suppose I have a table MyTable with a column some_date (date type of course) and I want to select the newest 3 months data (or x days).
What is the best way to achieve this?
Please notice that the date should not be measured from today but rather from the date range in the table (which might be older then today)
I need to find the maximum date and compare it to each row - if the difference is less than x days, return it.
All of this should be done with sqlalchemy and without loading the entire table.
What is the best way of doing it? must I have a subquery to find the maximum date? How do I select last X days?
Any help is appreciated.
EDIT:
The following query works in Oracle but seems inefficient (is max calculated for each row?) and I don't think that it'll work for all dialects:
select * from my_table where (select max(some_date) from my_table) - some_date < 10
You can do this in a single query and without resorting to creating datediff.
Here is an example I used for getting everything in the past day:
one_day = timedelta(hours=24)
one_day_ago = datetime.now() - one_day
Message.query.filter(Message.created > one_day_ago).all()
You can adapt the timedelta to whatever time range you are interested in.
UPDATE
Upon re-reading your question it looks like I failed to take into account the fact that you want to compare two dates which are in the database rather than today's day. I'm pretty sure that this sort of behavior is going to be database specific. In Postgres, you can use straightforward arithmetic.
Operations with DATEs
1. The difference between two DATES is always an INTEGER, representing the number of DAYS difference
DATE '1999-12-30' - DATE '1999-12-11' = INTEGER 19
You may add or subtract an INTEGER to a DATE to produce another DATE
DATE '1999-12-11' + INTEGER 19 = DATE '1999-12-30'
You're probably using timestamps if you are storing dates in postgres. Doing math with timestamps produces an interval object. Sqlalachemy works with timedeltas as a representation of intervals. So you could do something like:
one_day = timedelta(hours=24)
Model.query.join(ModelB, Model.created - ModelB.created < interval)
I haven't tested this exactly, but I've done things like this and they have worked.
I ended up doing two selects - one to get the max date and another to get the data
using the datediff recipe from this thread I added a datediff function and using the query q = session.query(MyTable).filter(datediff(max_date, some_date) < 10)
I still don't think this is the best way, but untill someone proves me wrong, it will have to do...

need query for this simple pulling data sql server

I am pulling data, but how do I pull from all data listed till an specific range?
Something like
select *
from xxx
where processdate = '6/30/2013'
this gives me all data only for that specific day..
I want data for all the days till 6/30/2013
I am having problems telling the sql to pull data available till 6/30/2013
This will probably be something really simple, but I cant seem to find the function to tell it
Or will I need to look at the data and see the earliest date, and then do a range
It is quite plain, use <= or < if you want to exclude this day.
select * from xxx where processdate <= '6/30/2013'

Get records as of today or up to a certain date in Oracle

Could somebody recommend the query to retrieve records up to today or certain dates?
I'm required to produce an Oracle report where user needs to enter a date and records up to that date will be shown.
I tried
select * from the_table where the_date <= sysdate
However it seems to produce an inaccurate result. What is the better query for this. For now I'm just playing around with sysdate. Later I will need to use a certain date keyed in by the user and all the records up to that date needs to be shown.
Any suggestions?
Sometimes you get inaccurate records because of little differences like minutes and seconds when two dates have the same day/month/year. Try the following
select * from the_table where TRUNC(the_date) <= sysdate
The TRUNC removes the minute and the seconds. Sometimes you get inaccurate records without using that