简体   繁体   English

SQL日期通配符

[英]SQL Date Wildcards

I am using sql to write up a query in which I am trying to choose records that have their date_from column (of type date/time) that satisfy these conditions: 我正在使用sql编写查询,在该查询中,我试图选择具有满足以下条件的date_from列(日期/时间类型)的记录:

  • it can be any year 可以是任何一年
  • months are june, july, august and september 几个月是六月,七月,八月和九月
  • AND IF IT IS JUNE, IT SHOULD CONSIDER FROM THE 15th JUNE ONWARDS 如果是6月,则应从6月15日起考虑

Here's what i've tried .. 这是我尝试过的..

Select name, surname
FROM employees emp
where month(emp.date_from) IN ('06' , '07' , '08', '09')

I have also tried using CASE but failed. 我也尝试使用CASE,但失败了。 Any help please? 有什么帮助吗?

WHERE MONTH(emp.date_from) IN (7,8,9)
   OR (MONTH(emp.date_from) = 6 AND DAY(emp.date_from) >= 15);

UPDATE UPDATE

Or as dates are treated as strings in MySQL 或者将日期视为MySQL字符串

WHERE RIGHT(emp.date_from, 5) BETWEEN '06-15' AND '09-30';

I don't know which would perform better however. 我不知道哪个会更好。

Sqlserver 2012 SQL服务器2012

SELECT 
  * 
FROM
  employees
WHERE 
  DateFromParts(2000,month(date_from),day(date_from))
    between '2000-06-15' and '2000-09-30'

Year 2000 is chosen because it is leap year and will handle leap year issues around 2000-02-29. 选择2000年是因为它是leap年,并且将处理2000-02-29年左右的leap年问题。

SELECT firstname, 
       lastname, 
       dateofbirth 
FROM   patient 
WHERE  Day(dateofbirth) >= CASE 
                             WHEN Month(dateofbirth) = '06' THEN 15 
                           END 
        OR Month(dateofbirth) IN ( '07', '08', '09' ) 
ORDER  BY Month(dateofbirth) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM