I 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
This question is related to
sql
sql-server
date
compare
Date format is yyyy-mm-dd. So the above query is looking for records older than 12Apr2013
Suggest you do a quick check by setting the date string to '2013-04-30', if no sql error, date format is confirmed to yyyy-mm-dd.
Try like this
select id,numbers_from,created_date,amount_numbers,SMS_text
from Test_Table
where
created_date <= '2013-12-04'
If You are comparing only with the date vale, then converting it to date (not datetime) will work
select id,numbers_from,created_date,amount_numbers,SMS_text
from Test_Table
where
created_date <= convert(date,'2013-04-12',102)
This conversion is also applicable during using GetDate() function
please try with below query
select id,numbers_from,created_date,amount_numbers,SMS_text
from Test_Table
where
convert(datetime, convert(varchar(10), created_date, 102)) <= convert(datetime,'2013-04-12')
Try to use "#" before and after of the date and be sure of your system date format. maybe "YYYYMMDD O YYYY-MM-DD O MM-DD-YYYY O USING '/ O \' "
Ex:
select id,numbers_from,created_date,amount_numbers,SMS_text
from Test_Table
where
created_date <= #2013-04-12#
You put <=
and it will catch the given date too. You can replace it with <
only.
Source: Stackoverflow.com