Oracle SQL
02. Oracle Single Row Functions
Eng:- Ahmed Ramadan , Eng:- Marwan Elkordey
Chapter02:Oracle Single Row Functions
Chapter Content :
Chapter02:Oracle Single Row Functions
What is Function ?
Chapter02:Oracle Single Row Functions
Character Functions
Lower
Upper
Initcap
Substr
instr
lpad
rpad
length
replace
concat
trim
Chapter02:Oracle Single Row Functions
Character Functions:
Lower – Upper – Initcap
Chapter02:Oracle Single Row Functions
Character Functions:
Length ( String )
Chapter02:Oracle Single Row Functions
Character Functions:
Replace ( String 1 , String2 , String3 )
Replace ( ‘ADAMS’ , ‘A’ , ‘B’)
In ADAMS replace each A with B
Chapter02:Oracle Single Row Functions
Character Functions:
Concat ( String 1 , String 2 )
Chapter02:Oracle Single Row Functions
Character Functions:
Concat ( String 1 , String 2 )
Chapter02:Oracle Single Row Functions
Character Functions:
Trim ( String from String)
Trim ( ‘A’ From ‘AAABMOALHKGGAA’ ) - BMOALHKGG
Chapter02:Oracle Single Row Functions
Character Functions:
Lpad ( String , Number , String)
Rpad ( String , Number , String )
Chapter02:Oracle Single Row Functions
Character Functions:
Substr( String , Number , Number)
Substr( String , Start Point, Length of characters )
Substr(‘Alexandria’ ,1,3) Ale
Substr (‘Alexandria’ , 3,4)exan
Chapter02:Oracle Single Row Functions
Character Functions:
Substr( String , Number , Number)
Substr( String , Start Point, Length of characters )
Substr ( Alexandria , 6)-ndria
Chapter02:Oracle Single Row Functions
Character Functions:
Substr( String , Number , Number)
Substr( String , Start Point, Length of characters )
Substr ( Alexandria , 6)-ndria
Chapter02:Oracle Single Row Functions
Character Functions:
instr( String , String, Number , Number)
Instr (‘Alexandria’ , ‘a’ ,1,1) 5
Instr (‘Alexandria’ , ‘a’ , 1 , 2 ) 10
Instr (‘Alexandria’ , ‘a’ , 6 , 1 ) 10
Instr (‘Alexandria’ , ‘a’ , -1 , 1 ) 10
Instr (‘Alexandria’ , ‘a’ , -1 , 2 ) 5
Chapter02:Oracle Single Row Functions
Character Functions:
instr( String , String, Number , Number)
Instr (‘Alexandria’ , ‘a’ ,1,1) 5
Instr (‘Alexandria’ , ‘a’ , 1 , 2 ) 10
Instr (‘Alexandria’ , ‘a’ , 6 , 1 ) 10
Instr (‘Alexandria’ , ‘a’ , -1 , 1 ) 10
Instr (‘Alexandria’ , ‘a’ , -1 , 2 ) 5
Chapter02:Oracle Single Row Functions
Number Functions:
round
trunc
mod
Chapter02:Oracle Single Row Functions
Number Functions:
Round ( Number1 , Number 2):
round (987.62 , 1) -----------987.6
round (987.625, 2) ----------987.63
round (987.625, 0) -----------988
round (987.625 ) -------------988
Chapter02:Oracle Single Row Functions
Number Functions:
Round ( Number1 , Number 2):
round (987.62 , -1)---------- 990
round (887.625 , -2)---------- 900
Chapter02:Oracle Single Row Functions
Number Functions:
Trunc ( Number1 , Number 2):
Trunc (987.62 ,1)-----------987.6
Trunc (987.625,2)----------987.62
Trunc (987.625,0)----------987
Trunc (987.625)------------987
Chapter02:Oracle Single Row Functions
Number Functions:
Trunc ( Number1 , Number 2):
Trunc (987.62 ,-1 )----------980
Trunc (987.625,-2 )----------900
Chapter02:Oracle Single Row Functions
Number Functions:
Mod ( Number1 , Number 2):
Number 1 / number2 ---- > reminder
Chapter02:Oracle Single Row Functions
Practices01 :
------------------------------------------------------------------------
• Write a Query Based on emp table to display the following output: select *
from emp
where substr ( ename , 1 , 1 ) in ( 'A' , 'S' , 'T') ;
Empno ename sal ? -----------------------------------------------------------------------
---------------------------------------------------- select *
7788 SCOTT 3000 *** from emp
where length ( ename) > 4
7789 Turner 2870 ** ------------------------------------------------------------------------
7790 Alen 1200 * select *
7791 Ward 5000 ***** from emp
where instr ( ename , 'L' , 1, 2 ) > 0
7792 AAAA 800 null ------------------------------------------------------------------------
-
select empno , ename , sal , trunc ( sal / 1000 ) ,
• Write a Query to display employee names in Capital letter lpad ( '*' , trunc ( sal / 1000 ) , '*')
• from emp
Write a Query to display employees with Double L in their names
------------------------------------------------------------------------
• Write a query to display employees with their names start with ( ‘S’ , ‘T’ , ‘A’ ) -
• Write a query to display employees with name length greater than 4
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Oracle Date Component Date Operations
Date - Date = number of days
DD MM YYYY hh12:MI:SS TZH:TZM Date + number = Date
-
DD : Day 1-31 to get the current date you can use sysdate function.
MM : Month 1-12
YYYY: year and century Ex.: 2012 1980 select sysdate from dual ;
hh24 : hours , hh12 (AM,PM)
MI : minuets ( 0- 59 )
SS : Seconds ( 0- 59 )
TZH :Time zone hours
TZM :Time zone minuets
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Oracle Date Operations
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Oracle Date Functions :
Months_between ( Date 1 , Date 2 )
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Oracle Date Functions :
add_months (date , number)
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Oracle Date Functions :
Last_day(date)
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Oracle Date Functions :
next_day( date , day_name)
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Using Round , Trunc with Dates
Round ( '10/08/2009' , 'year') ……….> 01/01/2010
Round ( '10/04/2009' , 'year') ……….> 01/01/2009
Trunc( '10/08/2009' , 'year') ……….> 01/01/2009
Trunc ( '10/04/2009' , 'year') ……….> 01/01/2009
Round ( '10/08/2009' , 'month') ……….> 01/08/2009
Round ( '20/08/2009' , 'month') ……….> 01/09/2009
Trunc(( '10/08/2009', 'month') ……….> 01/08/2009
Trunc ( '20/08/2009', 'month') ……….> 01/08/2009
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Using Round , Trunc with Dates
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Using Round , Trunc with Dates
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Using Round , Trunc with Dates
Chapter02:Oracle Single Row Functions
Oracle Date Functions
Using Round , Trunc with Dates
Chapter02:Oracle Single Row Functions
Oracle Conversion Functions
to_char
to_number
to_date
Chapter02:Oracle Single Row Functions
Oracle Conversion Functions
to_char ( Date , Format )
Date Format
------------
day : d dd day dy
month : mm month MON
year : yy yyyy year
hour : hh24 , hh12 ( AM , PM)
Minuit : mi
second : ss
Chapter02:Oracle Single Row Functions
Oracle Conversion Functions
to_char ( Date , Format )
Chapter02:Oracle Single Row Functions
Oracle Conversion Functions
to_char ( Number , Format )
number format :
-------------
$ L . , 9 0
Chapter02:Oracle Single Row Functions
Oracle Conversion Functions
TO_Number ( Number , Format )
Chapter02:Oracle Single Row Functions
Oracle Conversion Functions
TO_Date( date, Format )
Chapter02:Oracle Single Row Functions
Oracle General Functions
nvl (exp 1 , exp2)
-----------------------
if exp1 is null then return exp2 else return exp1
Chapter02:Oracle Single Row Functions
Oracle General Functions
nvl2( exp1 , exp2 , exp3)
----------------------------
if exp1 is null then return exp3 else return exp2
Chapter02:Oracle Single Row Functions
Oracle General Functions
Nullif (exp1 , exp2):
-------------------------
if exp1 = exp2 then return null else return exp1
Chapter02:Oracle Single Row Functions
Oracle General Functions
coalesce ( exp1 , exp2 , exp3 , ....)
if exp1 is null then test exp2 , if exp2 is null then test exp3 , ....
Chapter02:Oracle Single Row Functions
Oracle Conditional Functions
case Function :
Chapter02:Oracle Single Row Functions
Oracle Conditional Functions
case Function :
Chapter02:Oracle Single Row Functions
Oracle Conditional Functions
Decode Function :
Chapter02:Oracle Single Row Functions
Oracle Functions
Practices
• Write a Query to Display Employee Hire date , Employee Actual hire date if the actual hiredate increase by 6 months
Than hire date
• Write query to display for each employee the number of worked months , name the column Months_worked
• Write a query to diaply for each employee the number of days worked , name the column days_worked
• Write a query to display the hiredate in this format Example ( ‘Wednesday 22 January 1990 )
• Write a query to diaply the salary of the employee in this format example ( 003000.00)
• Write a query to display the salary of the employee , the comm and the sum of salary + comm ( avoid nulls )