Nitin soni
Nitin soni

Reputation: 1

MVC 5 and SQL Server date filter issue

I have datetime values in my database table like 05/05/2015 23:00:00. I am putting date filter in my query and try to fetch all the data of 05/05/2015 like this:

select * 
from table 
where date <= "05/05/2015".

It's not returning the records which have value 05/05/2015 23:00:00 in database.

Please suggest the way..

Upvotes: 0

Views: 54

Answers (1)

marc_s
marc_s

Reputation: 754538

Using this where clause here

where date <= "05/05/2015"

means: return every row with a date before 05/05/2015 (including the ones with 05/05/2015 00:00:00 - but nothing more).

If you want to get all records for that day, too, you should use

where date < '20150506'

I'd also recommend to use the ISO-8601 date format yyyyMMdd to prevent any regional settings from interfering with your strings representing dates.

And I would also recommend to use something more expressive than just date for your column name - in SQL Server 2008 and newer, DATE is a reserved T-SQL keyword, too - use something like HireDate, SaleDate or something that tells you want kind of date this is

Upvotes: 1

Related Questions