Basic Oracle Tutorial: single-row functions-branch Functions

Source: Internet
Author: User
Simple case is equivalent comparison (expression before when) SQLgt; selectename, sal, casedeptnowhen10then

Simple case is equivalent comparison (expression before when) SQLgt; select ename, sal, case deptno when 10 then

Branch Structure

Decode

If expr1
Then
Action1
Elif expr2
Then
Action2
...
Else
Default_action
Fi

Decode (expr1, search1, result1, search2, result2,..., default)
Only equivalent comparison can be performed.

SQL> select ename, deptno, decode (deptno,
10, 'aaa ',
20, 'bbbbbb ',
'Cccc') from emp order by 2;

ENAME DEPTNO DECO
------------------------
CLARK 10 AAAA
KING 10 AAAA
MILLER 10 AAAA
JONES 20 BBBB
FORD 20 BBBB
ADAMS 20 BBBB
SMITH 20 BBBB
SCOTT 20 BBBB
WARD 30 CCCC
TURNER 30 CCCC
ALLEN 30 CCCC
JAMES 30 CCCC
BLAKE 30 CCCC
MARTIN 30 CCCC

14 rows selected.

SQL>

Exercise:
Increase by part of the number (printed only) Department 10 up 10% part 20 up 20% other part up 30%


Decode
Select ename, sal, deptno,
Decode (deptno, 10, sal * 1.1 ,,
20, sal * 2.1,
Sal * 3.1)
From emp order by deptno;

Simple case

Select ename, sal, deptno,
Case deptno
When 10 then sal * 1.1
When 20 then sal * 2.1
The else sal * 3.1
End
From emp order by deptno;


Search case

Select ename, sal, deptno,
Case when deptno = 10 then sal * 1.1
When deptno = 20 then sal * 2.1
The else sal * 3.1
End
From emp order by deptno;

Case

Simple
Case expr when search1 then result1
When search2 then result2
....
Else default_result
End
Only equivalent comparison can be performed.

Simple case is equivalent comparison (expression before when)
SQL> select ename, sal, case deptno when 10 then 'aaa'
When 20 then 'bbb'
Else 'ccc'
End
From emp order by deptno;
SQL>

ENAME SAL CAS
-----------------------
CLARK 2450 AAA
KING 5000 AAA
MILLER 1300 AAA
JONES 2975 BBB
FORD 3000 BBB
ADAMS 1100 BBB
SMITH 800 BBB
SCOTT 3000 BBB
WARD 1250 CCC
TURNER 1500 CCC
ALLEN 1600 CCC
JAMES 950 CCC
BLAKE 2850 CCC
Martens 1250 CCC

14 rows selected.


The search case implements non-equivalent comparison (the expression is after the when)

Select ename, deptno case when deptno = 10 then 'aaa'
When deptno = 20 then 'bbb'
Else 'ccc'
End
From emp;

SQL> select ename, sal, case when sal <= 1000 then sal + 1
When sal> 1000 and sal <= 2000 then sal + 2
When sal> 2000 and sal <= 3000 then sal + 3
Else sal + 4
End "up_sal"
From emp order by sal;


Ename sal up_sal
------------------------------
SMITH 800 801
JAMES 950 951
ADAMS 1100 1102
WARD 1250 1252
MARTIN 1250 1252
MILLER 1300 1302
TURNER 1500 1502
ALLEN 1600 1602
CLARK 2450 2453
BLAKE 2850 2853
JONES 2975 2978
SCOTT 3000 3003
FORD 3000 3003
KING 5000 5004

14 rows selected.

SQL>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.