I want to do a query with dates this is my sample tsql:
select * from Bookings where StartTime = \'2/15/2014\'
the starttime has value \'2/15/2
'2/15/2014' can be interpreted different depending on your locale. Try using the ISO date literal '2014-02-15', which is independent of the locale.
select * from Bookings where StartTime = '2014-02-15'
Or if StartTime includes hours:
select * from Bookings where StartTime >= '2014-02-15' and StartTime < '2014-02'16'
I believe you could also do this:
select * from Bookings where StartTime::date = '2014-2-15'
Try like this
SELECT * FROM Bookings WHERE Convert(VARCHAR(10),StartTime,101) = Convert(Varchar(10),'2/15/2014',101)
If you are using SQL SERVER 2012
Try this
SELECT * FROM Bookings WHERE FORMAT(StartTime,'M/dd/yyyy') = FORMAT('2/15/2014','M/dd/yyyy')
The best way to do this is with a simple comparison:
select *
from Bookings
where StartTime >= cast('2014-02-15' as date) and StartTime < cast('2014-02-14' as date);
This is the safest method of comparison, because it will take advantage of an index on StartTime
. This property is called "sargability".
In SQL Server, casting to a date
should also be sargable, so you could also do:
select *
from Bookings
where cast(StartTime as date) = cast('2014-02-15' as date) ;