SQL search between two days without date and time

An attempt was made to search a date range database. The problem is that I cannot use the datetime column type in my database. To compensate, the dates are displayed as three columns. the Month column, the Day column, and the Year column. Here is my SQL query:

SELECT COUNT(*) 
  FROM `import` 
 WHERE `call_day` BETWEEN 29 AND 15 
   AND `call_month` BETWEEN 9 AND 10 
   AND `call_year` BETWEEN 2013 AND 2013

You can see what I am facing the problem. call_day needs to be searched between the 29th and 15th days. This will not work, because 15 is less than 29, but I need it to work, because a month in the future :)

Any thoughts / solutions? No. I can’t change the database in any way. Only for reading.

+4
source share
4 answers

, ,

SELECT * 
  FROM import
 WHERE STR_TO_DATE(CONCAT_WS('-', call_year, call_month, call_day), '%Y-%c-%e') 
       BETWEEN '2013-09-29' AND '2013-10-15'

SELECT *
  FROM import
 WHERE CONCAT(call_year, LPAD(call_month, 2, '0'), LPAD(call_day, 2, '0'))
       BETWEEN '20130929' AND '20131015'

SQLFiddle

, ,

SELECT *
  FROM import
 WHERE (call_year = 2013 AND 
        call_month = 9   AND 
        call_day BETWEEN 29 AND DAY(LAST_DAY('2013-09-01'))) -- or just 30
    OR (call_year = 2013 AND 
        call_month = 10 AND 
        call_day BETWEEN  1 AND 15)

SQLFiddle

, (, 2012-08-20 2013-10-15)

SELECT * 
  FROM import
 WHERE (call_year = 2012 AND 
        call_month = 8 AND
        call_day BETWEEN 20 AND 31)
    OR (call_year = 2012 AND 
        call_month BETWEEN 9 AND 12 AND
        call_day BETWEEN 1 AND 31)
    OR (call_year = 2013 AND 
        call_month BETWEEN 1 AND 9 AND 
        call_day BETWEEN  1 AND 31)
    OR (call_year = 2013 AND 
        call_month = 10 AND
        call_day BETWEEN 1 AND 15)

SQLFiddle

+3

, yyyymmdd, .

+5

PeterM, , , : ( * 10000) + ( * 100) + 8- "".

2013 *10000 = 20130000
9 * 100 =          900
                    15
              20130915

, , , - , , .

EDIT: !

+1
source
SELECT COUNT(*) FROM `import` 
WHERE CONCAT(CAST(`call_year`AS CHAR(4)) , RIGHT(CONCAT('00',CAST(`call_month`AS CHAR(2))),2) , RIGHT(CONCAT('00',CAST(`call_day`AS CHAR(2))),2))
BETWEEN '20130929' AND '20131015'
0
source

All Articles