Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative date range in T-SQL

Tags:

sql

tsql

I'm trying to select for all items where the createDt is in the last two weeks. I've tried this code but it doesn't work.

SELECT * FROM dbo.mytable
WHERE CreateDt > dateadd(d,-15,CreateDt)

Can someone tell me the correct way to do this?

like image 881
Ronald McDonald Avatar asked Dec 07 '22 17:12

Ronald McDonald


1 Answers

WHERE CreateDt > dateadd(d,-15,CreateDt)

should be

WHERE CreateDt > dateadd(d,-15,getdate())

Presumably.

All NOT NULL values of CreateDt will meet your current condition as you are comparing the column with its own value minus 15 days - not 15 days previous to the current date and time.

like image 179
Martin Smith Avatar answered Dec 30 '22 22:12

Martin Smith