Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data comparing inside decode function 1

Status
Not open for further replies.
Nov 16, 2002
24
US
The following query doesn't work. It seems decode function doesn't like < or > sign inside of it. But I need to compare clients' birth date to another date to get their age then and add them into a age group. Anybody knows how to do this?

select client_id,
decode(birth_date, MONTHS_BETWEEN(TO_DATE('01022003','DDMMYYYY'), TO_DATE(birth_date, 'DDMMYYYY')) < 13, 'Infant',
MONTHS_BETWEEN(TO_DATE('01022003','DDMMYYYY'), TO_DATE(birth_date, 'DDMMYYYY')) < 24, 'Toddler',
MONTHS_BETWEEN(TO_DATE('01022003','DDMMYYYY'), TO_DATE(birth_date, 'DDMMYYYY')) < 36, '2yr') Age
from clients;


 
try using a CASE STATEMENT

Like this

select client_id,
CASE WHEN MONTHS_BETWEEN(birth_date, sysdate) < 13
THEN 'Infant'
WHEN MONTHS_BETWEEN(birth_date, sysdate) < 24
THEN 'Toddler'
WHEN MONTHS_BETWEEN(birth_date, sysdate) < 36
THEN '2 year'
END AS AGE
FROM clients;

I think this will work only for 8.1.6 and higher.

Hope this helps
 
OK well try this

SELECT DECODE(TRUNC(MONTHS_BETWEEN(SYSDATE, TO_DATE('31-DEC-2000') )/12),0 , 'INFANT',1, 'TODDLER', 2, '2 YEAR') FROM DUAL


You might have to tweak it slightly but it will work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top