Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQl Plus: If..elsif or case statement

Posted on 2004-10-26
8
Medium Priority
?
4,418 Views
Last Modified: 2008-01-09
My student table has a field called key_stage which needs to be set to either 1,2,3 or 4 depending on date of birth (dob):

If dob between 02-jul-1996 and 01-jul-2000 then 1
If dob between 02-jul-1993 and 01-jul-1996 then 2
If dob between 02-jul-1990 and 01-jul-1993 then 3
If dob between 02-jul-1988 and 01-jul-1990 then 4

How can I run this with an if..elsif or case statement using
sql plus?



0
Comment
Question by:peadar86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Expert Comment

by:pratikroy
ID: 12412277
select case when dob between to_date('02-jul-96') and to_date('01-jul-00') then 1
                 when dob between to_date('02-jul-93') and to_date('01-jul-96') then 2
                 when dob between to_date('02-jul-90') and to_date('01-jul-93') then 3
                 when dob between to_date('02-jul-88') and to_date('01-jul-90') then 4 end as key_stage
from student;
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 12412661
Pre 9i (it gets real ugly):



select dob, DECODE('IN RANGE',
  DECODE(GREATEST(trunc(dob),   to_date('02-jul-1988','DD-MON-YYYY')), LEAST(trunc(dob), to_date('01-jul-1990','DD-MON-YYYY')),'IN RANGE','NOT IN RANGE'),'4',
  DECODE('IN RANGE',
  DECODE(GREATEST(trunc(dob), to_date('02-jul-1990','DD-MON-YYYY')), LEAST(trunc(dob), to_date('01-jul-1993','DD-MON-YYYY')),'IN RANGE','NOT IN RANGE'),'3',
  DECODE('IN RANGE',
  DECODE(GREATEST(trunc(dob),to_date('02-jul-1993','DD-MON-YYYY')), LEAST(trunc(dob),to_date('01-jul-1996','DD-MON-YYYY')),'IN RANGE','NOT IN RANGE'),'2',
  DECODE('IN RANGE',
  DECODE(GREATEST(trunc(dob),to_date('02-jul-1996','DD-MON-YYYY')), LEAST(trunc(dob),to_date('01-jul-2000','DD-MON-YYYY')),'IN RANGE','NOT IN RANGE'),'1',
                                                                     '???'))))  id_range
from tab1
/
0
 

Author Comment

by:peadar86
ID: 12419849

 
 I need to update the student table with the key_stage value rather than just selecting.
 How would I adapt these statements to do this?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 12420985
drop table tab1;
create table tab1(col1 char(1), col2 char(1));

insert into tab1(col1) values('a');
insert into tab1(col1) values('b');
insert into tab1(col1) values('c');
commit;


update tab1 t1 set col2=(
select
            case col1
            when 'a' then '1'
            when 'b' then '2'
            end
from tab1 t2 where t1.col1=t2.col1
)
;
0
 
LVL 9

Expert Comment

by:pratikroy
ID: 12421016
Both columns (DOB and KEY_STAGE) are in the same table ?

If so then try this :

update table1
set KEY_STAGE = (case when DOB between to_date('02-jul-96') and to_date('01-jul-00') then 1
                                   when DOB between to_date('02-jul-93') and to_date('01-jul-96') then 2
                                   when DOB between to_date('02-jul-90') and to_date('01-jul-93') then 3
                                   when DOB between to_date('02-jul-88') and to_date('01-jul-90') then 4 end);
0
 
LVL 9

Accepted Solution

by:
pratikroy earned 500 total points
ID: 12421032
in your case table1 is student, so :

update STUDENT
set KEY_STAGE = (case when DOB between to_date('02-jul-96') and to_date('01-jul-00') then 1
                                   when DOB between to_date('02-jul-93') and to_date('01-jul-96') then 2
                                   when DOB between to_date('02-jul-90') and to_date('01-jul-93') then 3
                                   when DOB between to_date('02-jul-88') and to_date('01-jul-90') then 4 end);
0
 

Author Comment

by:peadar86
ID: 12421426

 Thanks pratiloy

 That works fine.

 peadar86
0
 
LVL 9

Expert Comment

by:pratikroy
ID: 12421459
Glad I could help. Cheers !
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question