Get Previous Three Weeks data From a Startdate until Enddate.




declare @Startdt datetime='1/1/2014'
declare @Enddt datetime='1/5/2014'

declare @wk int=3
--select DATEADD(week, DATEDIFF(week,0,GETDATE()),-1)  --begining of this week

--...
Where dtColumn >= DATEADD(week, DATEDIFF(week,0,@Startdt)- @wk,-1)
And  dtColumn < @Enddt


 


declare @wk int=3

--....
WHERE dtColumn >= DATEADD(DAY, 1-DATEPART(WEEKDAY, @Startdt), Dateadd(day,-@wk*7,@Startdt))
AND dtColumn < @EndDate

 

https://social.msdn.microsoft.com/Forums/en-US/b7fc966c-59e1-4154-8f06-98e7f6a06ad6/please-help-me-to-fetch-last-3weeks-data-in-sql?forum=transactsql

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s