0% found this document useful (0 votes)
17 views

SQL - Single Function02

Uploaded by

manjari sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL - Single Function02

Uploaded by

manjari sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Number Functions

Round

0-4 - stay on the same number


5-9 - add one

6 4 8 5 . 3 7 2 9
-4-3-2-1 0 1 2 3 4

Trunc

Mod

select 6485.3729 from dual

select round(6485.3729,4) from dual

select round(6485.3729,3) from dual -6485.373


select round(6485.3729,2) from dual -6485.37
select round(6485.3729,1) from dual -6485.4
select round(6485.3729,0) from dual - 6485
select round(6485.3729) from dual - 6485
select round(6485.3729,-1) from dual - 6490
select round(6485.3729,-2) from dual - 6500
select round(6485.3729,-3) from dual - 6000
select round(6585.3729,-3) from dual -7000
select round(6485.3729,-4) from dual -10000
select round(4485.3729,-4) from dual -0

select trunc(6485.3729,3) from dual -6485.372


select trunc(6485.3729,2) from dual -6485.37
select trunc(6485.3729,1) from dual -6485.3
select trunc(6485.3729,0) from dual - 6485
select trunc(6485.3729) from dual - 6485
select trunc(6485.3729,-1) from dual - 6490
select trunc(6485.3729,-2) from dual - 6400
select trunc(6485.3729,-3) from dual - 6000
select trunc(6585.3729,-3) from dual -6000
select trunc(6485.3729,-4) from dual -0
select trunc(4485.3729,-4) from dual -0

select mod(1400,3) from dual

select mod(1800,3) from dual

Date Functions

add_months
months_between
next_day
last_day
round
trunc

select add_months(sysdate,5) from dual;


select add_months(sysdate,-3) from dual;

select add_months('30-Jun-23',2) from dual;

select sysdate+2 from dual;

select hire_Date,hire_Date+2 from employees


where employee_id=117

select '30-Jun-23'+2 from dual;

select trunc(months_between('09-dec-23','20-Jan-19'))
from dual;

select trunc(months_between('30-Jun-23','23-aug-23')) from dual;

select next_day(sysdate,'Friday') from dual;

select next_day(sysdate,'Saturday') from dual;

select last_day(sysdate) from dual;

select last_day('22-Feb-24') from dual;

round

month - 0- 15 - then same month's first date


16-31 - then next month's first date
year - Jan-Jun - same year's first date
Jul-Dec - next year's first date

select round (sysdate,'month') from dual;


select hire_Date,round (hire_date,'month') from employees where last_name='Abel';

select round (sysdate,'year') from dual;

select hire_Date,round (hire_date,'year') from employees where last_name='Abel';

trunc - same month's first date


same year's first date

select trunc(sysdate,'year') from dual;


select trunc(sysdate,'month') from dual;

Null Function

NVL(a,b) - if a is null then replace it by b


a and b has to be of same data type
NVL2 (a,b,c) - if a is not null then return b otherwise(if it is null) then return
c
a and (b,c) can be of different data types
COALESCE - (a,b,c...n) - if a is null then go to b then if b is also null goto c...
keep coalscing it till first not null is found

NULLIF - if a and b are both same the null otherwise return a


select last_name,salary,commission_pct from employees

select last_name,salary,commission_pct,salary+salary*commission_pct from employees

select last_name,salary,nvl(commission_pct,0),salary+salary*nvl(commission_pct,0)
from employees

select last_name,salary,nvl(commission_pct,'NO COMM') from employees

select
last_name,salary,commission_pct,nvl2(commission_pct,salary+salary*commission_pct,sa
lary) from employees

select last_name,salary,commission_pct,nvl2(commission_pct,'SALARY+COMM','NO COMM')


from employees

select
last_name,salary,commission_pct,manager_id,coalesce(commission_pct,manager_id,99999
9) from employees

select
first_name,last_name,length(first_name),length(last_name),nullif(length(first_name)
,length(last_name)) from employees

You might also like