Jim
Jim

Reputation: 1005

compare dates in mssql

So I have a date field in my database. Its of the type "Date". The value of the field is 2014-05-04 (yyyy-dd-mm).

How do I check, in sql, if the date is before or after today?

So far I have tied with:

SELECT * FROM table WHERE theDate > GetDate()

SELECT * FROM table WHERE CAST(theDate as Date) > CAST(GetDate() as Date)

How do you check if "2014-05-04" is before today?

Upvotes: 0

Views: 129

Answers (3)

Rohit Chaudhari
Rohit Chaudhari

Reputation: 757

SELECT * FROM table WHERE CONVERT(DATE, colDateTime)> CAST(GETTDATE() AS DATE)

This should work

Upvotes: 0

Tristan
Tristan

Reputation: 1024

SELECT 
TheDate,
CASE 
WHEN  TheDate < GETDATE() 
THEN 1
ELSE 0
END AS DateBeforeToday
FROM Table

Upvotes: 0

podiluska
podiluska

Reputation: 51514

The pointy bit of less than / greater than points to the lesser side.

So you want

SELECT * FROM table WHERE theDate < convert(date, GetDate())

Upvotes: 2

Related Questions