Update a running sql script to update the last 2 years based on today's date - sql

I currently have a simple sql script (in Oracle) that updates based a date range >='01JUN13'. I'm trying to modify this script to automatically update based on the last two years based on today's date. So the next month, I need the last two years of data greater than or equal to July 2013. Thanks.

It's not entirely clear from your question whether you want the date range to start from exactly two years ago, to the day , or from the start of the month.
Matching to the day:
select * from your_table
where date_col >= add_months(trunc(sysdate), -24)
/
Matching to the first day of the month:
select * from your_table
where date_col >= add_months(trunc(sysdate, 'MON'), -24)
/

Related

Get data from Current year and past two years

I'm trying to get data from Current year and last three years. (2022,2021,2020, 2019)
I've a year field which is number.
For now I hard-coded value in my code as Year >= 2019.
I need help on how to make this dynamic so in future I just get data of past three years from the current year(included).
(Eg: If the current year is 2025, I will get data of 2025,2024,2023,2022)
Can you try this?
SELECT *
FROM yourtable WHERE colyear >= YEAR( current_date() ) - 3;
CURRENT_DATE: https://docs.snowflake.com/en/sql-reference/functions/current_date.html
YEAR: https://docs.snowflake.com/en/sql-reference/functions/year.html

Snowflake SQL: Is there a way to select records between 2 years back and the current date?

I have seen a lot of questions similar to this but I have yet to see one that goes into detail of how to get records from two years back to today but include the start of the year two years back. Meaning I would like to create a function that will always give me results from January 1st two years back. For this year the results would come from 01-01-2020 to today’s date.
This is what I have so far, but in reality I am using it for a temporary table in my query.
SELECT *
FROM final
WHERE order_date BETWEEN DATEADD(‘year’, -2, current_date) AND current_date
You may use a combination of DATE_TRUNC and DATEADD to find January 1 of two years ago.
SELECT *
FROM final
WHERE order_date BETWEEN DATE_TRUNC('year', DATEADD('year', -2, current_date)) AND current_date;
What you had is close. Just truncate the the year.
You can see what happens in isolation:
select trunc('2021-03-14 08:24:12'::timestamp, 'YEAR');
-- Output: 2021-01-01 00:00:00.000
Adding to your SQL:
SELECT *
FROM final
WHERE order_date
BETWEEN trunc(DATEADD(‘year’, -2, current_date), 'YEAR') AND current_date
It is possible to construct arbitrary date using DATE_FROM_PARTS:
Creates a date from individual numeric components that represent the year, month, and day of the month.
DATE_FROM_PARTS( <year>, <month>, <day> )
For current_date:
SELECT DATE_FROM_PARTS(YEAR(current_date)-2,1,1);
-- 2020-01-01
Full query:
SELECT *
FROM final
WHERE order_date BETWEEN DATE_FROM_PARTS(YEAR(current_date)-2,1,1) AND current_date
This should be enough
where year(current_date)-year(order_date)<=2
But in case you have an order_date from the future
where year(current_date)-year(order_date)<=2 and order_date<=current_date

Sorting SQL Query By Date Column

I'm working on a project with a db that contains a date column for patient visits in the format of %m-%d-yyyy and need to sort so that it only pulls the rows where that date is within the last two weeks. I've tried a few different functions of convert, to_date, and can't seem to get anything to work.
I'm still very new to SQL and I don't know if this is a special case because I'm working with an oracle db
Not the full code, because it has dozens of queries and multiple joins (would that affect the date syntax?) but this is the format I'm trying for...
create table "Visits"
insert into "Visits" (
'John Doe',
'5/24/2021',
'Story about the visit',
'More room for story if needed')
select
"User_Name",
"Visit_Date",
"Visit_Narrative",
"Visit_Narrative_Overflow"
from "Visits"
where "Visits"."Visit_Date" >= TRUNC(SYSDATE) - 14
I'm working on a project with a db that contains a date column for patient visits in the format of %m-%d-yyyy
No, you don't have it in the format mm.dd.yyyy. A DATE data type value is stored within the database as a binary value in 7-bytes (representing century, year-of-century, month, day, hour, minute and second) and this has no format.
need to sort so that it only pulls the rows where that date is within the last two weeks.
You want a WHERE filter:
If you want to have the values that happened in the last 14 days then TRUNCate the current date back to midnight and subtract 14 days:
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE) - INTERVAL '14' DAY
or
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE) - 14
(or subtract 13 if you want today and 13 days before today.)
If you want it after Monday of last week then TRUNCate to the start of the ISO Week (which is always a Monday) and subtract 7 days:
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY
I ended up figuring it out based on an answer from another forum (linked below);
it appears that my original to_date() was incomplete without a second and operator in my where clause. This code is working perfectly
select
"User_Name",
"Visit_Date",
"Visit_Narrative",
"Visit_Narrative_Overflow"
from "SQLUser"."Visits"
where "SQLUser"."Visits"."Visit_Date" >= to_date('5/10/2021', 'MM/DD/YYYY')
and "SQLUser"."Visits"."Visit_Date" < to_date('5/24/2021', 'MM/DD/YYYY')

Trying to Look back 4 whole months in teradata with ADD_MONTHS function in sql statement

I'm trying to go back and retrieve counts for the last 4 full months. This is an example of what I have so far:
SELECT datecolumn, Count(datacolumnA) AS CountOfdatacolumnA, datacolumnB
FROM tableA
WHERE datacolumnB='AA' AND datecolumn >= ADD_MONTHS(CURRENT_DATE, -4)
My results show the last four months plus the current month, October in this case. The problem is that June isn't showing the correct count for the entire month. I'm only getting a partial count for the month.
You need to adjust to the start of the month. You can do this by subtracting the day of the month to get the '0th' of the month and then adding 1 to get the first. (I think dates in teradata are decimals with the int part being number of days since an epoch)
Select
datecolumn,
Count(datacolumnA) As CountOfdatacolumnA,
datacolumnB
From
tableA
Where
datacolumnB='AA' And
datecolumn >=
add_months(current_date, -4)
- extract(day from add_months(current_date, -4)) + 1

PostgreSQL - how to fetch rows higher then year since the date

I have in the DATE column saved a dates. I try to get those rows, that are higher since that date about a year, two years etc.
The year, two years is a parameter from my app. Is possible to get these rows by a query or should I do that on the application layer?
SELECT *
FROM your_table
WHERE the_date_column > current_date - interval '2' year
Just replace the 2 with your parameter value from the application.