Emel Uras Emel Uras - 1 year ago 58
SQL Question

Using dash while using like in where clause in SQL Server 2008 query

I have a table including such data in SQL Server 2008:

id TimeStamp
1 2016-02-19 11:52:01 AM
2 2016-02-21 11:30:05 AM

I am trying to select such data saved in the date 19 February 2016. I am using the query in the below, but it is returning result without any values.

select *
from myTable
where [TimeStamp] like '2016-02-19%'

However, when I write the query below, I can get all the data saved in 2016. So I believe something is wrong with the dashes maybe?

select *
from myTable
where [TimeStamp] like '%2016%'

How can I fix my issue, what is my error?

Answer Source

Do not use like with dates. Use simple comparisons:

where [TimeStamp] >= '2016-02-19' and [TimeStamp] < '2016-02-20'