简体   繁体   中英

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:

  • 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

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. 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

Or as dates are treated as strings in MySQL

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

I don't know which would perform better however.

Sqlserver 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.

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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