0 votes
in Sql by

Query comparing dates in SQLI have a table with dates that all happened in the month November. I wrote this query
select id,numbers_from,created_date,amount_numbers,SMS_text 
from Test_Table
where 
created_date <= '2013-04-12'
This query should return everything that happened in month 11 (November) because it happened before the date '2013-04-12' (in December)
But it's only returning available dates that happened in days lesser than 04 (2013-04-12)
Could it be that it's only comparing the day part? and not the whole date?
How to fix this?
Created_date is of type date
Date format is by default yyyy-dd-MM

1 Answer

0 votes
by
Instead of '2013-04-12' whose meaning depends on the local culture, use '20130412' which is recognized as the culture invariant format.

If you want to compare with December 4th, you should write '20131204'. If you want to compare with April 12th, you should write '20130412'.

The article Write International Transact-SQL Statements from SQL Server's documentation explains how to write statements that are culture invariant:

Applications that use other APIs, or Transact-SQL scripts, stored procedures, and triggers, should use the unseparated numeric strings. For example, yyyymmdd as 19980924.

Related questions

0 votes
asked Nov 7, 2021 in Sql by rajeshsharma
0 votes
asked Apr 15, 2023 in JAVA by kamalkhandelwal29
...