Marc
Marc

Reputation: 2859

sql compare datetime today

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.

Upvotes: 4

Views: 9425

Answers (2)

Andrew
Andrew

Reputation: 27294

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)

Upvotes: 8

Chris Latta
Chris Latta

Reputation: 20560

You want the DateAdd function to manipulate dates and the GetDate function to get the current date:

SELECT * FROM MyTable WHERE [MyDateTimeRow] < DateAdd(dd, -3, GetDate())

Upvotes: 2

Related Questions