4

I want to know if a date (Year-Month-Day) is between a day of the month (Month-Day).

For example, I want to know if 'April 2, 2013' is between 'April 2' and 'April 19'.

I can easily do this of the range has a year value. Howver, without a year, I need ideas how to do this.

Some examples of what I'm trying to achieve:

SELECT 1 WHERE '2013-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-03-02' BETWEEN '04-01' AND '04-19'; -- result is 1

Thanks

4

3 回答 3

3

你可以利用DATE_FORMAT().

例子

SELECT 
*
FROM
 table
WHERE
  DATE_FORMAT(date,'%m-%d') BETWEEN '04-01' AND '04-19'

编辑

SELECT 
   *
FROM
   table
WHERE
   STR_TO_DATE(date, '%m-%d') BETWEEN '04-01' AND '04-19'
于 2013-08-27T04:33:24.163 回答
0

以下是使用 oracle 的工作

select * from table_name where to_char(date_column, 'MM-DD') BETWEEN '08-01' AND '08-14'
于 2013-08-27T05:10:17.750 回答
0

接受的答案很好,但如果你想像@tony-brix 所说的那样从 12 月到 2 月选择,它就不起作用。DATE_FORMAT您可以使用两个功能扩展该答案。一个从十二月到年底,另一个从年初到二月。

SELECT 
*
FROM
 table
WHERE
  DATE_FORMAT(date,'%m-%d') BETWEEN '12-01' AND '12-31'
OR
   DATE_FORMAT(date,'%m-%d') BETWEEN '01-01' AND '02-01'
于 2022-02-26T22:10:51.990 回答