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:
Select * from [User] U where convert(varchar(10),U.DateCreated, 120) = '2014-02-07'
This is a better way:
Select * from [User] U
where U.DateCreated >= '2014-02-07' and U.DateCreated < dateadd(day,1,'2014-02-07')
see: What does the word “SARGable” really mean?
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.
the best practice with date and time ranges is to avoid BETWEEN and to always use the form:
WHERE col >= '20120101' AND col < '20120201' This form works with all types and all precisions, regardless of whether the time part is applicable.
http://sqlmag.com/t-sql/t-sql-best-practices-part-2 (Itzik Ben-Gan)
can
sometimes compensate.
Commented
Aug 29, 2014 at 9:34
date literal
format is 'YYYYMMDD'. Data types: date or datetime or time or datetime2 or smalldatetime do NOT have a format all as they are stored as numbers. For date literals 'YYYY-MM-DD' is NOT entirely safe there is one locale setting that can misinterpret that sequence to be YYYY-DD-MM
Commented
Sep 24, 2015 at 23:39
If you are on SQL Server 2008 or later you can use the date datatype:
SELECT *
FROM [User] U
WHERE CAST(U.DateCreated as DATE) = '2014-02-07'
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:
I've just tried this on a large table, with a secondary index on the date column as per @kobik's comments and the index is still used, this is not the case for the examples that use BETWEEN or >= and <:
SELECT *
FROM [User] U
WHERE CAST(U.DateCreated as DATE) = '2016-07-05'
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
with 2014-02-07 00:00:00.000
for equality). that's why result of comparison is false
Therefore, 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'
.
between
with equals at both ends (>= and <=) e.g. select * from x where col_1 between 'A' and 'M'
includes A and M (plus B to L of course). That inclusive nature of between
is terrific for something like a document index; but for date/time ranges it is a disaster because it creates overlaps. I know using between looks great, but in truth an SQL between
just does not work properly for date ranges. Always use a combination of >= with < as in U.DateCreated >='2014-02-07' and U.DateCreated < '2014-02-08'
Commented
Sep 15, 2015 at 8:16
between
is OK. your suggestion to use >=
and <
is no doubt preferable over between
for date ranges. I just want to say that we should consider time also when we compare dates.
Commented
Sep 16, 2015 at 12:28
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:
SELECT CONVERT(DATE,GETDATE())
OR
Select * from [User] U
where CONVERT(DATE,U.DateCreated) = '2014-02-07'
Please try this. This query can be used for date comparison
select * from [User] U where convert(varchar(10),U.DateCreated, 120) = '2014-02-07'
You can use LIKE
statement instead of =
. But to do this with DateStamp you need to CONVERT
it first to VARCHAR:
SELECT *
FROM [User] U
WHERE CONVERT(VARCHAR, U.DateCreated, 120) LIKE '2014-02-07%'