Vikram Vikram - 2 years ago 85
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
the records based on the current date like
. 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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download