Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql compare datetime today

Tags:

sql

datetime

tsql

I hope anyone can translate my abstract query.

I want to select * from TABLE where ( [MYDATETIMEROW] < (TODAY - 3 Days)).

Does I have to Convert, cast or use datepart or anything else?.. im confused.

Are there simple rules? I would'nt have problems to do that with linq but simple sql I learned just hardly.

Thank you and best regards.

like image 815
Marc Avatar asked Dec 29 '22 06:12

Marc


1 Answers

In simple terms:

Select * from Table where MyDateTimeRow < dateadd(dd,-3,getdate())

But using getdate() will provide both a date and a time, experience says that this is unlikely to be exactly what you want - you might want to strip the time down and just consider the date portion

Select * From Table where MyDateTimeRow < dateadd(dd, datediff(dd, 0, getdate()) - 3, 0)
like image 54
Andrew Avatar answered Jan 14 '23 08:01

Andrew