Select * from [User] U
where U.DateCreated = '2014-02-07'
but in the database the user was created on 2014-02-07 12:30:47.220
and when I only put '2014-02-07'
It does not show any data
Select * from [User] U
where U.DateCreated = '2014-02-07'
but in the database the user was created on 2014-02-07 12:30:47.220
and when I only put '2014-02-07'
It does not show any data
DON'T be tempted to do things like this:
This is a better way:
see: Sargable(the page was removed from Wikipedia)EDIT + There are 2 fundamental reasons for avoiding use of functions on data in the where clause (or in join conditions).
2014-02-07
. It is far more efficient to alter the criteria to suit the data instead."Amending the criteria to suit the data" is my way of describing "use
SARGABLE
predicates"And do not use between either.
http://sqlmag.com/t-sql/t-sql-best-practices-part-2 (Itzik Ben-Gan)
Please try this. This query can be used for date comparison
If you are on SQL Server 2008 or later you can use the date datatype:
It should be noted that if date column is indexed then this will still utilise the index and is SARGable. This is a special case for dates and datetimes.
You can see that SQL Server actually turns this into a > and < clause:
Of-course this is an old thread but to make it complete.
From SQL 2008 you can use DATE datatype so you can simply do:
You can use
LIKE
statement instead of=
. But to do this with DateStamp you need toCONVERT
it first to VARCHAR:According to your query
Select * from [User] U where U.DateCreated = '2014-02-07'
SQL Server is comparing exact date and time i.e (comparing
2014-02-07 12:30:47.220
with2014-02-07 00:00:00.000
for equality). that's why result of comparison is falseTherefore, While comparing dates you need to consider time also. You can use
Select * from [User] U where U.DateCreated BETWEEN '2014-02-07' AND '2014-02-08'
.