Oracle SQL for Previous Year Data - sql

How can i write SQL which will fetch the data for current year -4
e.g. if i run this sql today so it should give me the data from 2014 to till date
if i run this sql in 2019 so it should give me the data from 2015 to till date
please assist.

You can make use of interval
SELECT *
FROM yourtable
WHERE yourcolumn >= trunc(sysdate, 'yyyy') - interval '4' year
AND yourcolumn < trunc(sysdate, 'yyyy');
This will produce results from 2015/Jan/01 to till date; If you need exactly 4 years worth of data then you need to tweak this SQL to include date and month along with the year.

Related

Bigquery SQL - Exclude rows/data for current Financial Year

I have a query in which I want to add a WHERE clause to exclude data for current Financial Year (without hardcoding it)
Note: FinancialYear 1st July to 30 June
SELECT * from TABLE A
WHERE FinancialYear != Current_Financial_Year
Based on a date column I have extracted Financial Year as below but not sure how to check if its current financial year and then exclude it using the WHERE clause above
extract(year from date_add(CalendarDate, interval 6 month)) as FinancialYear
You can compare to the current financial year by using similar logic on current_date:
where extract(year from date_add(CalendarDate, interval 6 month)) <>
extract(year from date_add(Current_Date, interval 6 month))
The best approach would be to use the following query
select * from Table_A where FinancialYear NOT LIKE '%2021%'
All you need to change is the year and you will get the respective data :)
Regards :)

Retrieve specific year in the format of "2019-05-04 11:20:22.697" in SQL query

How can I retrieve specific year in the format of “2019-05-04 11:20:22.697” in SQL query? I need the date between 2019 and 2020.
select * from date where date between '2019-01-01' and '2020-01-01'
If you want a query to just target the year 2019, then use:
SELECT *
FROM yourTable
WHERE date >= '2019-01-01' AND date < '2020-01-01';
This will include the entire 2019 calendar year, up to, but not including, January 1 of 2020. Note also that the above WHERE clause, as written, is sargable, meaning that the above query could take advantage of an index on the date column for doing the search.

I want to select data from last 3 days partition in oracle

In Oracle SQL, It is possible to get data from 3 days partitions only. For example, If I run SQL on 15-Aug-2019, My SQL should only look on 13,14,15 Day partitions or If I run SQL on 16-Aug-2019, My SQL should only look on 14,15,16 Day partitions.
TIA
I think that you are just looking for the following where clause:
where my_date_col >= trunc(sysdate) - 2 and my_date_col < trunc(sysdate) + 1
Where date_col is the column that holds the date that you want to filter.
For current sysdate December 30th at 10h22, trunc(sysdate) gives you December 30th at midnight, and trunc(sysdate) - 2 produces December 28th at midnight.

How to write a SQL query to retrieve all those customers whose age in months is more than 200 months?

How to write a SQL/Oracle query to retrieve all those customers whose age in months is more than 200 months?
I have a exam on Monday but I am having some confusion with months and dates calculation.
You can use a Query like this for MySQL:
SELECT *
FROM yourTable
WHERE bithdayField <= NOW() - INTERVAL 200 MONTH;
The logic is the same (the date is older than today minus 200 months), but the actual SQL is usually different, because DBMSes have a large variation of syntax in the date/time area.
Standard SQL & MySQL:
WHERE datecol < current_date - interval '200' month
Oracle:
WHERE datecol < add_months(current_date, -200)
In fact Oracle also supports the Standard SQL version, but it's not recommended, because you might get an invalid date error when you do something like '2018-03-31' - interval '1' month. This is based on a (dumb) Standard SQL rule which MySQL doesn't follow: one month before March 31 was February 31, oops, that date doesn't exists.
In Oracle DB, there are two nice functions : months_between and add_months
been used for these type date calculations. For your case, you may use one of the following :
select id, name, surname
from customers
where months_between(trunc(sysdate),DOB)>200;
or
select id, name, surname
from customers
where add_months(trunc(sysdate),-200)>DOB;
demo

Oracle SQL Retrieve Data From End of Month - 16th And 15th - 1st

Alright so I am trying to retrieve data a field we will call DATE_OF_ENTRY and the field is like this.
Example DATE_OF_ENTRY Data
28-NOV-15
So I need to use this field in a script that will be running twice a month to pull certain records. Basically when it's the 16th day of the current month I want all the records from the 1st-15th to be pulled up. When I run this script on the 1st of the next month I want all the records from the 16th-End of last month.
What I am using now
WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') AND DATE_OF_ENTRY < trunc(sysdate)
The problem with this statement is that it works on the 1st for the 16th to End of the last month, but on the 16th it gets data from the prior month still.
Any help is appreciated!
Using TRUNC() function with MONTH parameter will get the first day of the month.
Using TRUNC() function with DATE_OF_ENTRY will remove the TIME part.
Use + operator to add days to a DATE
SELECT TRUNC(sysdate, 'MONTH') firstDay,
TRUNC(sysdate, 'MONTH') + 15 Day15,
*
FROM yourTable
WHERE TRUNC(DATE_OF_ENTRY) >= TRUNC(sysdate, 'MONTH')
AND TRUNC(DATE_OF_ENTRY) <= TRUNC(sysdate, 'MONTH') + 15