tags:

views:

64

answers:

4

How to select people within age limit 19 t0 25 if the current date is less than '2011-01-01' and age limit between 19 to 26 if the current date is greater than or equal to'2011-01-01'

A: 

You can use simple AND-OR logic to acheive this.

Select * from <people_table>
  where ( (age between 19 and 25) and 
            (current_date < to_date('2011-01-01','YYYY-MM-DD'))
        )
     or (  (age between 19 and 26) and 
           (current_date >= to_date('2011-01-01','YYYY-MM-DD'))
        )

Unless of course, by current_date, if you mean sysdate, you'll have...

Select * from <people_table>
  where ( (age between 19 and 25) and 
            (sysdate < to_date('2011-01-01','YYYY-MM-DD'))
        )
     or (  (age between 19 and 26) and 
           (sysdate >= to_date('2011-01-01','YYYY-MM-DD'))
        )
Rajesh
A: 
select people from table 
where 
( age between 19 and 25 and getdate() < '2011-01-01' ) or
( age between 19 and 25 and getdate() >= '2011-01-01' )
subt13
A: 

Well, in MySQL, CURDATE() will get you the current date. Then, you could sue DATEDIFF() and see if the difference is in a range you want. For example,

SELECT * FROM table WHERE DATEDIFF(year,CURDATE(),date) > 18 AND DATEDIFF(year, CURDATE(), date) < 26;

Siege898
A: 

Try this -

select * from people_table
    where age > 19 and age <= 
    case 
    when (getdate() < '2011-01-01') then 25
    when (getdate() >= '2011-01-01') then 26
    end
Sachin Shanbhag