user3606198
user3606198

Reputation: 97

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:

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?

Upvotes: 1

Views: 2364

Answers (3)

user3884116
user3884116

Reputation: 9

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) 

Upvotes: -1

t-clausen.dk
t-clausen.dk

Reputation: 44316

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.

Upvotes: 0

Arth
Arth

Reputation: 13110

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.

Upvotes: 3

Related Questions