2

I have two dates say start_date = 2013-04-12 and end_date = 2013-04-30
and my table contains row with start_date = 2013-04-12 and end_date = 2013-04-16

I want to fetch records whose start date is greater 2013-04-12 and end_date is less than 2013-04-30. Which includes above record having 2013-04-16 as end_date

I tried with this query

SELECT * FROM TABLE_NAME WHERE (start_date <='2013-04-12' AND end_date >='2013-04-30') which dont give any result however if i put end_date 2013-04-15 it works fine

4

5 回答 5

5
SELECT
    *
FROM
    (
        SELECT
            '2013-04-12' as `start_date`,
            '2013-04-16' as `end_date`
    ) `sub`
WHERE
    (`start_date` BETWEEN '2013-04-12' AND '2013-04-30')
    AND
    (`end_date` BETWEEN '2013-04-12' AND '2013-04-30')

BETWEEN is more reliable

于 2013-04-12T10:15:29.140 回答
3

If you want to have start_date GREATER OR EQUAL 2013-04-12 and end_date LESSER OR EQUAL, than your operators are wrong:

start_date <='2013-04-12' AND end_date >='2013-04-30'

you should use >= for start_date and <= for end_date like this:

start_date >='2013-04-12' AND end_date <='2013-04-30'
于 2013-04-12T10:11:40.357 回答
1

I think you are using wrong operators ... start_date greater than(>=) end_date less than equal to (<=)

SELECT * FROM TABLE_NAME WHERE (start_date >='2013-04-12' AND end_date <='2013-04-30')
于 2013-04-12T10:14:34.017 回答
0

Try this,

SELECT * FROM TABLE_NAME WHERE Convert(varchar,start_date,101) >'04/12/2013') AND convert(varchar,end_date,101) <'04/30/2013')
于 2013-04-12T10:11:28.173 回答
0

I would do: SELECT * FROM table_name WHERE start_date BETWEEN '2013-04-12' AND '2013-04-30'

于 2013-04-12T10:16:28.293 回答