SQL “between” not inclusive

后端 未结 8 1682
情歌与酒
情歌与酒 2020-11-30 17:05

I have a query like this:

SELECT * FROM Cases WHERE created_at BETWEEN \'2013-05-01\' AND \'2013-05-01\'

But this gives no results even tho

8条回答
  •  我在风中等你
    2020-11-30 17:45

    You need to do one of these two options:

    1. Include the time component in your between condition: ... where created_at between '2013-05-01 00:00:00' and '2013-05-01 23:59:59' (not recommended... see the last paragraph)
    2. Use inequalities instead of between. Notice that then you'll have to add one day to the second value: ... where (created_at >= '2013-05-01' and created_at < '2013-05-02')

    My personal preference is the second option. Also, Aaron Bertrand has a very clear explanation on why it should be used.

提交回复
热议问题