Oracle SQL Where Clause against a date column - sql

I have a DATE column with a date in it but I need to query it to find records less than 30 days old.
START_DATE
----------
01-AUG-2010
09-AUG-2010
22-AUG-2010
09-SEP-2010
Query:
SELECT START_DATE
WHERE START_DATE < 30;
I know it is simple Query in ORACLE SQL but i am doing something wrong.

Use:
SELECT t.start_date
FROM YOUR_TABLE t
WHERE t.start_date > SYSDATE - 30
SYSDATE is Oracle's syntax to get the current date and time
In Oracle, you can do date arithmetic in the context of days, so SYSDATE - 30 means "current date, subtract thirty days" to get a date that is thirty days in the past
If you want to evaluate the date based on thirty days as of midnight, use the TRUNC function:
SELECT t.start_date
FROM YOUR_TABLE t
WHERE t.start_date > TRUNC(SYSDATE) - 30
Don't run TRUNC on the column - that will render an index on the column useless, ensuring a table scan.

SELECT t.start_date
FROM YOUR_TABLE t
WHERE t.start_date > SYSDATE - INTERVAL '30' DAY;
INTERVAL is more portable than assuming that you can add or subtract days, although I've noticed some slight differences in the INTERVAL syntax between Oracle and PostgreSQL.

WHERE START_DATE > SYSDATE - 1
or perhaps
WHERE TRIM(STARTDATE) > TRIM(SYSDATE) - 1

Related

Obtain data from Friday, Saturday and Sunday if today is Monday SQL Oracle

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
);

Search Last 7 days excluding today Oracle SQL

I have the below code to which I want to return the last 7 days excluding today (for example from 5th May - 11th May as opposed to 5th May - 12th May)
What else would I be able to include to acheive this?
SELECT *
FROM TABLE_1
WHERE DATE_TIME >= SYSDATE -7
You want to have a range that starts from 7 days before midnight today and ends before midnight today:
SELECT *
FROM table_name
WHERE date_time >= TRUNC(sysdate) - 7
AND date_time < TRUNC(sysdate);
This should work:
SELECT *
FROM TABLE_1
WHERE DATE_TIME >= SYSDATE -7
AND TRUNC(DATE_TIME) != TRUNC(SYSDATE)
The TRUNC is needed to strip the time portion of the date column and sysdate.
Note that DATE_TIME >= SYSDATE -7 will include the time portion of SYSDATE and substract 7 days. If you run the query at 10AM, do you want to include rows that have date_time = sysdate - 7 at 9AM too ? If so it is better to add a TRUNC there too DATE_TIME >= TRUNC(SYSDATE) -7.

Select rows where max date is less than December 31st of the previous year

I am using Oracle SQL developer. I am trying to select rows where max date is less than December 31st of the previous year. I tried this query below but kept getting an error.
Select ORG_ID, STS_DTE
from Table1
Group By RC_ORG_ID
Having MAX(STS_DTE)< '31-Dec-' || extract (year from SYSDATE-1)
I would use trunc():
having MAX(STS_DTE) < trunc(sysdate, 'YYYY') - interval '1 day'
I'm not sure if you want sysdate - interval '1' day.

Fetch yesterday details using extract in SQL

consultation(patient_id, cdate, doctor_id)
I want the details of patients which was treated yesterday using extract
But I am getting error
ORA-00911: invalid character
My code:
select *
from consultation
where extract(day from cdate) = extract(day from sysdate) - '1';
You may avoid extract and simply use:
select *
from consultation
where trunc(cdate) = trunc(sysdate-1)
Here I use trunc to remove the time part; also, notice that by using extract to compare the day, you will get not only records of yesterday, but even records from the past months.
If you need to get all the records where the day is 6 (assuming that sysdate is May, 7), no matter the month or year, you can use:
where extract(day from cDate) = extract(day from sysdate -1)
Prefer not to use EXTRACT on the date column, it is less efficient, especially if there's an index or partition on the date column. simply use TRUNC on SYSDATE
select * from consultation where
cdate >= TRUNC(sysdate) - 1
AND cdate < TRUNC(SYSDATE)
you need to put integer 1 not string '1'
select * from consultation where
extract(day from cdate) = extract(day from sysdate) - 1;

how to get current year to date and prior year to date in sql

Suppose my date range is current: 01-jan-2015 to 17-Feb-2015.
I need the data for 01-jan-2014 to 17-02-2014 also.
how to write the query in sql?
i want to get the data for these CYD and pYD.
Here is an example in Oracle :
select sysdate - interval '1' year from dual;
The key word 'interval' also exist in other SQL language, like postgres.
To select those ranges of dates from, say, a calendar table, I'd probably write this in standard SQL.
select cal_date from calendar
where cal_date between date '2015-01-01'
and current_date
or cal_date between date '2015-01-01' - interval '1' year
and current_date - interval '1' year
order by cal_date;
If I wanted to identify rows in each range, I'd add literal values.
select 'cyd', cal_date
from calendar
where cal_date between date '2015-01-01'
and current_date
union all
select 'pyd', cal_date
from calendar
where cal_date between date '2015-01-01' - interval '1' year
and current_date - interval '1' year
order by cal_date;