You are on page 1of 12

functions:

character function:
sql> select ename,lower(ename),upper(ename),initcap(ename),instr(ename,'a'),
concat(ename,job),

2 length(ename) from emp where deptno=10;

ename lower(enam upper(enam initcap(en instr(ename,'a') concat(ename,job) length(ename)

---------- ---------- -------- ---------- ----------------


------------------- -------------

clark clark clark clark 3 clarkmanager


5

king king king king 0 kingpresident


4

miller miller miller miller 0 millerclerk


6

concat command:
sql> select ename,concat(ename,concat(',',job)) from emp where deptno=10;

ename concat(ename,concat(
---------- --------------------
clark clark,manager
king king,president
miller miller,clerk

instad of using concat we may use ' || ' :

sql> select ename ||','|| job from emp where deptno=10;

ename||','||job
-----------------------
clark,manager
king,president
miller,clerk

fetching string:
sql> select ename,substr(ename,2,3),substr(ename,2) from emp where deptno=10;

ename sub substr(en


----------- ------- ------------------
clark lar lark
king ing ing
miller ill iller

sql> select ' kg ' from dual;

'kg'
----
kg

sql> select length(' kg ') from dual;

length('kg')
------------
6
length from left, right and center:
sql> select length(ltrim(' kg ')),length(rtrim(' kg ')),length(trim(' kg ')) from dual;

length(ltrim('kg')) length(rtrim('kg')) length(trim('kg'))


------------------------------- -------------------------------- -----------------------------
4 4 2

lpad and rpad:

sql> select ename,lpad(ename,10,'*'),rpad(ename,10,'*') from emp where deptno=10;

ename lpad(ename rpad(ename


------------ --------------------- ---------------------
clark *****clark clark*****
king ******king king******
miller ****miller miller****

selecting 2nd char as 'a' :


sql> select ename,substr(ename,2,3) from emp where substr(ename,2,3) like 'a%';

ename sub
----------- ------
ward ard
martin art
james ame

sql> select ename,replace(ename,'k','k') from emp where deptno=10;

ename replace(en
------------ ---------------------
clark clark
king king
miller miller

number function:
round command:
sql> select abs(-90),round(123.361,0),round(123.361),round(123.361,1),
round(123.361,2),round(123.361,-1),
2 round(123.36,-2) from dual;

abs(-90) round(123.361,0) round(123.361) round(123.361,1) round(123.361,2)


round(123.361,-1) round(123.36,-2)
------------- ---------------------------- ------------------------- -----------------------------
--------------------------- ----------------------------- ---------------------------

90 123 123 123.4 123.36


120 100

truncate command:

sql> select abs(-90),trunc(123.361,0),trunc(123.361),trunc(123.361,1),trunc(123.361,2),


2 trunc(123.361,-1),trunc(123.36,-2) from dual;

abs(-90) trunc(123.361,0) trunc(123.361) trunc(123.361,1) trunc(123.361,2) trunc(123.361,-


1) trunc(123.36,-2)

------------- -------------------------- ----------------------- -------------------------- --------------------------


--------------------------- --------------------------

90 123 123 123.3 123.36


120 100

floor & ceil command:


sql> select floor(99.9),ceil(99.9),floor(-99.9),ceil(-99.9) from dual;

floor(99.9) ceil(99.9) floor(-99.9) ceil(-99.9)


------------------ -------------- ------------------- ----------------
99 100 -100 -99

date & conversion function:


sql> select sysdate from dual;

sysdate
---------------
07-feb-07

sql> select to_char(sysdate,'dd dy day ddd mm mon month year yyyy rr yy hh24:mi:ss') from
dual;

to_char(sysdate,'dd dy day ddd mm mon month year yyyy rr yy


hh24:mi:ss')
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------
07 wed wednesday 038 02 feb february two thousand seven 2007 07
07 10:08:43

sql> select hiredate,to_char(hiredate,'day yyyy') from emp where deptno = 10;

hiredate to_char(hireda
--------- --------------
09-jun-81 tuesday 1981
17-nov-81 tuesday 1981
23-jan-82 saturday 1982

sql> select hiredate,to_char(sysdate-hiredate)/365 from emp where deptno = 10;

hiredate to_char(sysdate-hiredate)/365
--------- -----------------------------
09-jun-81 25.686355
17-nov-81 25.2452591
23-jan-82 25.0616974

selecting a particular record using like clause:

sql> select hiredate,to_char(hiredate,'day yyyy') from emp where to_char(hiredate,'day') like


'fri%';

hiredate to_char(hireda
--------- --------------
01-may-81 friday 1981
20-feb-81 friday 1981

sysdate is minused with hiredate:

* (trunc command is used to round the output value.)


sql> select sysdate,hiredate,trunc(sysdate-hiredate) from emp;

sysdate hiredate trunc(sysdate-hiredate)


--------- --------- -----------------------
08-feb-07 17-nov-81 9214
08-feb-07 01-may-81 9414
08-feb-07 09-jun-81 9375
08-feb-07 02-apr-81 9443
08-feb-07 28-sep-81 9264
08-feb-07 20-feb-81 9484
08-feb-07 08-sep-81 9284
08-feb-07 03-dec-81 9198
08-feb-07 22-feb-81 9482
08-feb-07 03-dec-81 9198
08-feb-07 17-dec-80 9549

sysdate hiredate trunc(sysdate-hiredate)


--------- --------- -----------------------
08-feb-07 09-dec-82 8827
08-feb-07 12-jan-83 8793
08-feb-07 23-jan-82 9147
14 rows selected.

sql> select to_date ('15/8/1947','dd/mm/yyyy') from dual

to_date('
---------
15-aug-47

sql> select to_char(to_date('15/8/1947','dd/mm/yyyy'),'day') from dual;

to_char(t
---------
friday

creating a table input as date:

sql> create table ss(doj date);

table created.

sql> insert into ss values('15-jan-90');

1 row created.

sql> select doj,to_char(doj,'yyyy') from ss;

doj to_c
--------- ----
19-jan-90 1990
19-jan-90 1890

sql> select doj,to_char(doj,'rr') from ss;

doj to
--------- --
19-jan-90 90
19-jan-90 90

sql> select doj,to_char(doj,'yy') from ss;

doj to
--------- --
19-jan-90 90
19-jan-90 90

---------------------------------------------------------

sql> select to_char(to_date('15/8/1947','dd/mm/rr')) from dual;

to_char(t
---------
15-aug-47

sql> select hiredate,months_between(sysdate,hiredate) from emp where deptno=10;

hiredate months_between(sysdate,hiredate)
---------------- ------------------------------------------------------------------
09-jun-81 307.981282
17-nov-81 302.723218
23-jan-82 300.52967

sql> select hiredate,months_between(sysdate,hiredate)/12 from emp where deptno=10;

hiredate months_between(sysdate,hiredate)/12
---------------- ----------------------------------------------------------------------
09-jun-81 25.6651132
17-nov-81 25.2269411
23-jan-82 25.0441454

sql> select sysdate,add_months(sysdate,16) from dual;

sysdate add_month
--------------- ---------------------
08-feb-07 08-jun-08

group function:
salary:

sql> select max(sal),min(sal),avg(sal),count(sal),sum(sal) from emp;

max(sal) min(sal) avg(sal) count(sal) sum(sal)


---------- ---------- ---------- ---------- ----------
5000 800 2073.21429 14 29025

commision:

sql> select max(comm),min(comm),avg(comm),count(comm),sum(comm) from emp;

max(comm) min(comm) avg(comm) count(comm) sum(comm)


---------- ---------- ---------- ----------- ----------
1400 0 550 4 2200

for counting hiredate:

sql> select count(hiredate) from emp;

count(hiredate)
---------------
14

for counting :

' * ' is only used for count.


sql> select max(sal),min(sal),avg(sal),count(*) from emp;

max(sal) min(sal) avg(sal) count(*)


---------- ---------- ---------- ----------
5000 800 2073.21429 14

using ' group by ' clause:

sql> select deptno,max(sal),min(sal),count(*) from emp group by deptno;

deptno max(sal) min(sal) count(*)


---------- ---------- ---------- ----------
10 5000 1300 3
20 3000 800 5
30 2850 950 6

using ' where ' with group by clause:

sql> select deptno,max(sal),min(sal),count(*) from emp where sal=5000 group by deptno;

deptno max(sal) min(sal) count(*)


---------- ---------- ---------- ----------
10 5000 5000 1

=> number of column should not allowed.

sql> select deptno,max(sal),min(sal),count(*) from emp where sal=5000,deptno=10 group by


deptno;
select deptno,max(sal),min(sal),count(*) from emp where sal=5000,deptno=10 group by deptno
*
error at line 1:
ora-00933: sql command not properly ended

=>left hand side must be a column name.

sql> select deptno,max(sal),min(sal),count(*) from emp where group by deptno;


select deptno,max(sal),min(sal),count(*) from emp where group by deptno
*
error at line 1:
ora-00936: missing expression

using ' having ' clause:

having clause is use 2 restrict the content of using clause.


sql> select deptno,max(sal),min(sal),sum(sal),count(*) from emp where sal>1000 group by
deptno
2 having sum(sal)>10000;

deptno max(sal) min(sal) sum(sal) count(*)


---------- ---------- ---------- ---------- ----------
20 3000 1100 10075 4

using ' order by ' clause:

sql> select deptno,max(sal),min(sal),sum(sal),count(*) from emp where sal>1000 group by


deptno
2 having sum(sal)>10000
3 order by sum(sal);

deptno max(sal) min(sal) sum(sal) count(*)


---------- ---------- ---------- ---------- ----------
20 3000 1100 10075 4

group

displaying sum of salary:

sql> select sum(sal) from emp group by deptno;

sum(sal)
----------
8750
10875
9400

=> which columns are used in the group clause,that columns must be used in the select part.

sql> select deptno,sal,count(*) from emp group by deptno,sal;

deptno sal count(*)


---------- ---------- ----------
10 1300 1
10 5000 1
10 1450 1
20 800 1
20 1100 1
20 3000 2
20 2975 1
30 1500 1
30 1600 1
30 950 1
30 1250 2

deptno sal count(*)


---------- ---------- ----------
30 2850 1

12 rows selected.

sql> select deptno,sal,count(*) from emp group by deptno;


select deptno,sal,count(*) from emp group by deptno
*
error at line 1:
ora-00979: not a group by expression

sql> select deptno,sal,count(*) from emp group by deptno,sal


2 having count(*)>1;

deptno sal count(*)


---------- ---------- ----------
20 3000 2
30 1250 2

to display salary * commision:

sql> select comm,sal,sal*comm,nvl(comm,0),nvl(to_char(comm,0),'no comm') from emp;

comm sal sal*comm nvl(comm,0) nvl(to_


---------- ---------- ---------- ----------- -------
5000 0 no comm
2850 0 no comm
2450 0 no comm
2975 0 no comm
1400 1250 1750000 1400 ##
300 1600 480000 300 ##
0 1500 0 0 0
950 0 no comm
500 1250 625000 500 ##
3000 0 no comm
800 0 no comm

comm sal sal*comm nvl(comm,0) nvl(to_


---------- ---------- ---------- ----------- -------
3000 0 no comm
1100 0 no comm
1300 0 no comm

14 rows selected.

nvl2:

sql> select comm,nvl2(comm,comm,0),nvl2(to_char(comm),'comm','no comm') from emp;

comm nvl2(comm,comm,0) nvl2(to


----------- ---------------------------------- -------------
0 no comm
0 no comm
0 no comm
0 no comm
1400 1400 comm
300 300 comm
0 0 comm
0 no comm
500 500 comm
0 no comm
0 no comm

comm nvl2(comm,comm,0) nvl2(to


---------- ----------------- -------
0 no comm
0 no comm
0 no comm

14 rows selected.
nullif:

sql> select sal,comm,nullif(sal,comm) from emp;

sal comm nullif(sal,comm)


---------- ---------- ----------------
5000 5000
2850 2850
2450 500 2450
2975 2975
1250 1400 1250
1600 300 1600
1500 0 1500
950 950
1250 500 1250
3000 3000
800 800
3000 3000
1100 1100
1300 500 1300

14 rows selected.

update commision:

sql> update emp set comm=500 where deptno=10;

3 rows updated.

sql> select * from emp where deptno=10;

empno ename job mgr hiredate sal comm


---------- ---------- --------- ---------- --------- ---------- ----------
deptno
----------
7839 king president 17-nov-81 5000 500
10

7782 clark manager 7839 09-jun-81 2450 500


10

7934 miller clerk 7782 23-jan-82 1300 500


10

distinct:

sql> select distinct comm,deptno from emp;

comm deptno
---------- ----------
0 30
300 30
500 10
500 30
1400 30
5000 10
20
30

8 rows selected.

without distinct:

sql> select comm,deptno from emp;

comm deptno
---------- ----------
5000 10
30
500 10
20
1400 30
300 30
0 30
30
500 30
20
20
20
20
500 10

14 rows selected.

decode:

sql> select sal,decode(sal,5000,sal*5,3000,sal*4,sal) from emp;

sal decode(sal,5000,sal*5,3000,sal*4,sal)
---------- -------------------------------------
5000 25000
2850 2850
2450 2450
2975 2975
1250 1250
1600 1600
1500 1500
950 950
1250 1250
3000 12000
800 800
3000 12000
1100 1100
1300 1300

14 rows selected.

case:
sql> select sal,case when sal=5000 then sal*5 when sal=3000 then sal*4 else sal end from emp;

sal casewhensal=5000thensal*5whensal=3000thensal*4elsesalend
---------- --------------------------------------------------------
5000 25000
2850 2850
2450 2450
2975 2975
1250 1250
1600 1600
1500 1500
950 950
1250 1250
3000 12000
800 800
3000 12000
1100 1100
1300 1300

14 rows selected.

You might also like