Sql-server – SQL date and time query not importing all

MySQLquerysql server

I have the below table and when i run this query:

SELECT *
FROM table1
WHERE Date <= "11/15/2018 12:00:00 AM"

it only retrieves the dates 15,14,13,12,11,10 and 1 of Nov 2018 .
How can i solve this? Thanks!

I tried changing the format of the date in the WHERE part of the query to "11/15/18 12:00:00 AM", it doesn't work.

enter image description here

Best Answer

This looks like a case where the date column isn't a date at all, but rather a string.

You aren't getting Nov 2 through Nov 9 because the sorting on the string and calculating if it's less than 11/1.... in alphanumeric order.

Ideally, dates will be stored as dates, and then your existing query will work. This will also ensure that dates are valid dates, and you don't end up with data for 32 February, or even random strings.

If you can't change the type of the column, you'll have to cast the database values to a date to do the comparison (which will hurt performance, but get you the results you expect).