Está en la página 1de 21

WEEK-4A

The Built-In Functions-2

Numeric Functions
Date Functions
Conversion Functions
2-The Built-in Numeric Functions
Name Description
ABS Returns the absolute value of the number.

ACOS Returns the inverse cosine.

COS Returns the cosine.

ASIN Returns the inverse sine.

SIN Returns the sine.

ATAN Returns the inverse tangent.

TAN Returns the tangent.

CEIL Returns the smallest integer greater than or equal to the specified
number.
FLOOR Returns the largest integer equal to or less than the specified
number.
EXP (n) Returns e raised to the nth power, where e = 2.71828183...

LN (a) Returns the natural logarithm of a.


LOG (a, b) Returns the logarithm, base a, of b.
MOD (a, b) Returns the remainder of a divided by b.

POWER (a, b) Returns a raised to the bth power.

ROUND (a, [b]) Returns a rounded to b decimal places.

SIGN (a) Returns 1 if a is positive, 0 if a is 0, and -1 if a is less than 0.

SQRT Returns the square root of the number.

TRUNC (a, [b]) Returns a truncated to b decimal places.


ABS
ABS(-1)  1 ABS(23)  23 ABS(-25) 25

CEIL
The CEIL ("ceiling") function returns the smallest integer greater than or equal to the
specified number.

CEIL(2.2)  3 CEIL(5.9)  6 CEIL(-5.9) 5

FLOOR
he FLOOR function, the opposite of the CEIL function, returns the largest integer that is less
than or equal to the input number.

FLOOR(2.2)  2 FLOOR(5.9)  5 FLOOR(-5.9) -6

EXP(N)  eN
The number e (approximately equal to 2.71828) is the base of the system of natural logarithms

EXP(2)  7.38 EXP(3)  20.08 EXP(2)  7.38


SIN
SIN(30)  -0,988031624092862 WRONG !!!

SIN(0,523598775598299)  0.5 CORRECT

ASIN 30 DEGREE = 0,523598775598299 RADIAN

ASIN(0.5)  0,523598775598299

COS 30 DEGREE = 0,523598775598299 RADIAN

COS(30)  0,154251449887584 WRONG !!!

COS(0,523598775598299)  0,866025403784439 CORRECT

ACOS
ACOS(0,866025403784439)  0,523598775598299
LN
The LN function returns the natural logarithm of the input. The specification for the LN function is:

LN(1)  0 LN(10)  2.30 ABS(-1)  ERROR

LOG
The LOG function returns the base-b logarithm of the input value.

LOG(10,1)  0 LOG(10,1000)  3 LOG(10,-1)  ERROR

MOD
The MOD function returns the remainder of one number when divided by a second number.

MOD(20,3)  2 MOD(5,2)  1 MOD(6,3) 0

POWER NM POWER(N,M)
The POWER function raises the first argument to the power indicated by the second argument.

POWER(2,3)  8 POWER(5,2)  25 POWER(-2,3)  -8


ROUND
ROUND function returns a number rounded to a certain number of decimal places.
ROUND(2.25)  2 ROUND(2.25,1)  2,3 ROUND(2.25,2)  2,25
ROUND(124,-1)  120 ROUND(125,-1)  130

SIGN
The SIGN function returns the sign of the input number.

SIGN(-25)  -1 SIGN(0)  0 SIGN(25)  1

SQRT
The SQRT function returns the square root of the input number.

SQRT(25)  5

TRUNC
The TRUNC function truncates the first argument to the number of decimal places specified by the
second argument.

TRUNC (53.46)  53 TRUNC (53.6789,1) 53.6 TRUNC (53.6789,3) 53.678


3-The Built-In Date Functions

Name Description

ADD_MONTHS Adds the specified number of months to a date.

LAST_DAY Returns the last day in the month of the specified


date.
MONTHS_ BETWEEN Calculates the number of months between two dates.

NEXT_DAY Returns the date of the first weekday specified that is


later than the date.
SYSDATE Returns the current date and time in the Oracle
Server.
ADD_MONTHS
ADD_MONTHS ('12-JAN-1995', 3)  12-APR-1995

LAST_DAY
LAST_DAY ('12-JAN-99')  31-JAN-1999

MONTHS_BETWEEN
MONTHS_BETWEEN ('31-MAR-1995', '28-FEB-1994‘)  13

SYSDATE
Display the current date
4-The Built-In Conversion Functions

Name Description

TO_CHAR Converts a number or date to a string.

TO_DATE Converts a string to a date.

TO_NUMBER Converts a string to a number.


SELECT TO_NUMBER(’23’)+TO_NUMBER(‘2’) FROM DUAL

SELECT ’23’||‘2’ FROM DUAL


Other Built-In functions
Name Description
Decode compares expression to the search_x expressions and, if matches, returns
DECODE result_x. If not, returns default.
DECODE ( expression,expression1, result1,
expression2,result2,
default_result)

CASE It has the functionality of an IF-THEN-ELSE statement.


CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END

USER USER function returns the user_id from the current Oracle session.
SELECT USER FROM DUAL; >>>>> SCOTT
UID function returns the id number for a user's session (the user who is
UID currently
logged in).
SELECT UID FROM DUAL; >>>>> 54

COALESCE The coalesce function returns the first non-null expression in the list. If all
expressions evaluate to null, then the coalesce function will return null.

The syntax for the coalesce function is:


coalesce( expr1, expr2, ... expr_n )
1-Write an SQL statement which returns the employees and their salary which added percentage of salary as shown
below.
30% to MANAGER, 40% to ANALYST,50% to CLERK, 60% to SALESMAN, 70% to PRESIDENT

SELECT ENAME, DECODE(JOB,'MANAGER',SAL+SAL*30/100,


'ANALYST',SAL+SAL*40/100,
'CLERK',SAL+SAL*50/100,
'SALESMAN',SAL+SAL*60/100,
'PRESIDENT',SAL+SAL*70/100) AS NEW FROM EMP;
1-Write an SQL statement which returns the employees and their salary which added percentage of salary as shown
below.
30% to MANAGER, 40% to ANALYST,50% to CLERK, 60% to SALESMAN, 70% to PRESIDENT
SELECT ENAME,CASE
WHEN (JOB='MANAGER') THEN SAL+SAL*30/100
WHEN (JOB='ANALYST') THEN SAL+SAL*40/100
WHEN (JOB='CLERK') THEN SAL+SAL*50/100
WHEN (JOB='SALESMAN') THEN SAL+SAL*60/100
WHEN (JOB='PRESIDENT') THEN SAL+SAL*70/100 ELSE SAL END AS NEW
TABLE: GUIDE

SELECT NAME, COALESCE(ADDRESS1,ADDRESS2,ADDRESS3)


AS ADDRESS FROM GUIDE
1-Write an SQL statement which returns

a ) sin(300 ) b) log10 150 c) log 2 1024 d )a cos(1) e)a tan(1)

f )a sin(0.5) g ) cos(450 )
sin 30
h)  tan 2 45  log3 600  ln 6  812
cos 70
2-Write an SQL statement which returns

3.56 >>> ? Function>>> 3


5.286 >>> ? Function>>> 5.29
7.12 >>> ? Function>>> 7
7.12 >>> ? Function>>> 8
5.45612>>>? Function>>> 5.45
567>>>>>>? Function>>> 570
Use AIRPLANES table for 3rd ,4th and 5th tasks
3-Write an SQL statement which return ‘ONE’ if line_number=1,’TWO’ if line_number=2
or ‘MORE THAN TWO’ if line_number is greater than 2.
4-Write an SQL statement which returns the full name of customer_id as shown in the
figure.

AAL American Airlines


ILC Intl. Leasing Corp.
NWO Northwest Orient
SAL Southwest Airlines
SWA Sweptwing Airlines
USAF U.S. Air Force

5-Write an SQL statement which returns


‘ONE’, if line_number between 1 and 10,
‘BIG’, if line_number is between 11 and 50,
‘BIGGER’, if line_number is greater than 50.
6-Write an SQL statement which returns values using coalesce in order col1,col2,col3
Table: test
Col1 col2 col3
-----------------------------------
1 3
1 5
6 1
1
2 1
1) select sin(30*3.14/180) from emp;
select log(10,150) from emp;
select log(2,1024) from emp;
select acos(1)*180/3.14 from emp;
select atan(1)*180/3.14 from emp;
select asin(0.5)*180/3.14 from emp;
select cos(45*3.14/180) from emp;
select ABS( sin(30*3.14/180) / cos(70*3.14/180) + TAN(45*3.14/180)*TAN(45*3.14/180) + log(3,600) ) + ln(6)+power(8,12)
FROM DUAL;
2) select floor(3.56) from emp;
select round(5.286,2) from emp;
select floor(7.12) from emp;
select ceil(7.12) from emp;
select trunc(5.45612,2) from emp;
select round(567,-1) from emp;
3) select decode(line_number,1,'ONE',2,'TWO','MORE THAN TWO') from airplanes;
4) select decode(customer_id,'AAL','American Airlines','ILC','INtl. Leasing Corp','NWO','Northwest Orient','SAL','Southwest
Airlines','SWA','Sweptwing Airlines','USAF','U.S. Air Force') from airplanes;
5)select CASE WHEN LINE_NUMBER BETWEEN 1 AND 10 THEN 'ONE'
WHEN LINE_NUMBER BETWEEN 11 AND 15 THEN 'BIG'
WHEN LINE_NUMBER>15 THEN 'BIGGER' END FROM AIRPLANES;
6) SELECT COALESCE(COL1,COL2,COL3) FROM TEST;

También podría gustarte