# Tutorial :Need a SQL query to select people within a specific age limit based on the current date ### Question:

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'

### Solution:1

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'))          )  ``

### Solution:2

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;

### Solution:3

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »