Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime BETWEEN statement not working in SQL Server

I have the following query,

SELECT * FROM LOGS 
WHERE CHECK_IN BETWEEN CONVERT(datetime,'2013-10-17') AND CONVERT(datetime,'2013-10-18')

this query not returning any result, but the following query return the result,

SELECT * FROM LOGS WHERE CHECK_IN >= CONVERT(datetime,'2013-10-17')

why the first query not returning any result? If I did any mistake pls correct me.

like image 778
Able Alias Avatar asked Oct 18 '13 14:10

Able Alias


People also ask

How can I get date between two dates in SQL?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.

How can I get data between two timestamps in SQL?

As stated above, the format of date and time in our table shall be yyyy:mm: dd hh:mm: ss which is implied by DATETIME2. The time is in a 24-hour format. Syntax: SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';

Does between include both dates in SQL?

The SQL BETWEEN Operator The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

How use not between in SQL Server?

SQL NOT BETWEEN Operator for Numeric ValueThe SQL NOT BETWEEN operator is used for getting the values as part of result set which is outside of the range specified by the BETWEEN operator.


4 Answers

Do you have times associated with your dates? BETWEEN is inclusive, but when you convert 2013-10-18 to a date it becomes 2013-10-18 00:00:000.00. Anything that is logged after the first second of the 18th will not shown using BETWEEN, unless you include a time value.

Try:

SELECT 
* 
FROM LOGS 
WHERE CHECK_IN BETWEEN 
    CONVERT(datetime,'2013-10-17') 
    AND CONVERT(datetime,'2013-10-18 23:59:59:998')

if you want to search the entire day of the 18th. I set miliseconds to 998 because SQL Server was pulling in 2013-10-19 00:00:00:0000 in the query.

SQL DATETIME fields have milliseconds. So I added 999 to the field.

like image 69
Skerkles Avatar answered Oct 04 '22 07:10

Skerkles


Does the second query return any results from the 17th, or just from the 18th?

The first query will only return results from the 17th, or midnight on the 18th.

Try this instead

select * 
from LOGS 
where check_in >= CONVERT(datetime,'2013-10-17') 
and check_in< CONVERT(datetime,'2013-10-19')
like image 27
podiluska Avatar answered Oct 04 '22 07:10

podiluska


From Sql Server 2008 you have "date" format.

So you can use

SELECT * FROM LOGS WHERE CONVERT(date,[CHECK_IN]) BETWEEN '2013-10-18' AND '2013-10-18'

https://docs.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql

like image 26
maurox Avatar answered Oct 04 '22 09:10

maurox


You don't have any error in either of your queries. My guess is the following:

  • No records exists between 2013-10-17' and '2013-10-18'
  • the records the second query returns you exist after '2013-10-18'
like image 44
apomene Avatar answered Oct 04 '22 09:10

apomene