Crazy Programmer
Crazy Programmer

Reputation: 194

sql <> not working with datetime

I Have a simple query

SELECT * 
FROM SlabRateDetail 
WHERE Fromdate <> '01/04/2010 12:00:00 AM'

but when I run this query I see 01/04/2010 12:00:00 AM in my results.

Any Idea why? I have even tried using !=

Upvotes: 2

Views: 85

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300728

Use unambiguous ISO8601 like dates when specifying date literals (YYYY/MM/DD)

e.g.

SELECT * FROM SlabRateDetail 
WHERE Fromdate <> '2010-04-01 12:00:00 AM' 

I believe you are seeing dates that are 2010/01/04

By default SQL Server uses the US date convention of MM/DD/YYYY for literals (depending on your server settings)

Upvotes: 5

StackTrace
StackTrace

Reputation: 9416

Try this

SELECT * FROM SlabRateDetail WHERE CONVERT(VARCHAR(10), Fromdate, 106) <> CONVERT(VARCHAR(10), '01/04/2010 12:00:00 AM', 106)

Upvotes: 0

Related Questions