Why date comparison in sql does not work. Please help

I am trying to extract some records from a table, but when I use OR instead of AND, it returns me some records, but not in another case. dates are precisely indicated in the table. What mistake am I making?

select newsid,title,detail,hotnews 
from view_newsmaster
where  datefrom>=CONVERT(datetime, '4-22-2010',111) 
  AND dateto<=CONVERT(datetime, '4-22-2010',111)
+3
source share
4 answers

I think you have your “from” and “to” around the wrong way ...

select newsid,title,detail,hotnews from view_newsmaster 
 where  dateto>=CONVERT(datetime, '4-22-2010',111) AND datefrom<=CONVERT(datetime, '4-22-2010',111) 
+2
source

It's hard to say without any model data from your table.

, datefrom , dateto - - , AND -.

dateto CONVERT(datetime, '4-23-2010',111) - , CONVERT(datetime, '4-22-2010',111) - ( ) 22 , .

+2

, 00:00:00, , , .

:

where  datefrom>=CONVERT(datetime, '4-22-2010 00:00:00',111) 
  AND dateto<=CONVERT(datetime, '4-22-2010 23:59:59',111)

, BETWEEN:

where  fDateField BETWEEN '4-22-2010' AND '4-23-2010'

, - + 1, .. , 23:59:59

+1

AND, ( , ).

When you use OR, you will get dates more than the first, and lower than the second.

These are two different conditions, so it’s reasonable that your results will be different.

What are you trying to achieve? What should return this request?

0
source

Source: https://habr.com/ru/post/1742243/


All Articles