Vikram Vikram - 5 months ago 20
SQL Question

Compare Current Date with LIKE in MS SQL Server

I have the date field in this format

2016-06-09 10:56:29.000
in one of the table. I want to
select
the records based on the current date like
2016-06-09
. I am trying the below query but not showing any records.

select * from dbo.Accounts where createDate like CAST(GETDATE() as DATE)


I know I am doing something wrong here. Any SQL Expert, please help me.

Answer

Do not use like. Just cast the values as a date:

select *
from dbo.Accounts
where cast(createDate as date) = CAST(GETDATE() as DATE);

like is a string function. You should compare dates using a date function, such as =. If you use like, then the value is implicitly being converted to a string, using whatever localization parameters are in effect.

Comments