Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi all,

If I want to run the query today(12/2/2017) then my data should be inbetween 2016 January 1st (always previous year) to December 31st (always previous year) --I don't want to hard code dates.

if I want to run the query on jan 1st 2018 or in any day in 2018 then my data should be inbetween 2017 January 1st(always previous year) to 2017 December 31st(always previous year).

if I want to run the query in 2019 then my data should be inbetween 2018 January 1st (always previous year)to 2018 December 31st (always previous year).

how to write query, please advice.

What I have tried:

Hi all,

If I want to run the query today(12/2/2017) then my data should be inbetween 2016 January 1st (always previous year) to December 31st (always previous year) --I don't want to hard code dates.

if I want to run the query on jan 1st 2018 or in any day in 2018 then my data should be inbetween 2017 January 1st(always previous year) to 2017 December 31st(always previous year).

if I want to run the query in 2019 then my data should be inbetween 2018 January 1st (always previous year)to 2018 December 31st (always previous year).

how to write query, please advice.
Posted
Updated 8-Jan-18 11:01am

Look at SQL Functions:
SQL Server DATEADD() Function[^] and YEAR (Transact-SQL) | Microsoft Docs[^] or SQL Server DATEPART() Function[^]
Get the year, construct a DATE of Jan first that year. Use that with DATEADD -1 day for last day, and -1 year for start day.
 
Share this answer
 
I resolved it :
tween ADD_MONTHS (TRUNC (SYSDATE,'YEAR'), -12)
and ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), -1 ) +30 )
thank you OriginalGriff.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900