week-4a built in functions

Upload: kenan

Post on 30-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Week-4a Built in Functions

    1/21

    The Built-In Functions-2

    Numeric Functions

    Date Functions

    Conversion Functions

    WEEK-4A

  • 8/9/2019 Week-4a Built in Functions

    2/21

    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 specifiednumber.

    FLOOR Returns the largest integer equal to or less than the specified number.

    EXP (n) Returns e raised to the n th power, where e = 2.71828183...

    LN (a) Returns the natural logarithm of a .

    2-The Built-in Numeric Functions

  • 8/9/2019 Week-4a Built in Functions

    3/21

    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 b th 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.

  • 8/9/2019 Week-4a Built in Functions

    4/21

    ABSABS(-1) 1 ABS(23) 23 ABS(-25) 25

    CEILThe 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

    EXP(N) e NThe 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

    FLOORhe 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

  • 8/9/2019 Week-4a Built in Functions

    5/21

    ASINASIN(0.5 ) 0,523598775598299

    30 DEGREE = 0,523598775598299 RADIAN

    SINSIN( 30 ) -0,988031624092862 WRONG !!!

    SIN( 0,523598775598299 ) 0.5 CORRECT

    COSCOS( 30 ) 0,154251449887584 WRONG !!!

    COS( 0,523598775598299 ) 0,866025403784439 CORRECT

    30 DEGREE = 0,523598775598299 RADIAN

    ACOSACOS(0,866025403784439 ) 0,523598775598299

  • 8/9/2019 Week-4a Built in Functions

    6/21

    LNThe 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

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

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

    POWER N M 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

    MODThe 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

  • 8/9/2019 Week-4a Built in Functions

    7/21

    ROUNDROUND 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

    SIGNThe SIGN function returns the sign of the input number.

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

    TRUNCThe 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

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

    SQRT(25) 5

  • 8/9/2019 Week-4a Built in Functions

    8/21

    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 islater than the date.

    SYSDATE Returns the current date and time in the Oracle Server.

    3-The Built-In Date Functions

  • 8/9/2019 Week-4a Built in Functions

    9/21

    ADD_MONTHSADD_MONTHS ('12-JAN-1995', 3) 12-APR-1995

    LAST_DAYLAST_DAY ('12-JAN-99') 31-JAN-1999

    SYSDATEDisplay the current date

    MONTHS_BETWEENMONTHS_BETWEEN ('31-MAR-1995', '28-FEB-1994) 13

  • 8/9/2019 Week-4a Built in Functions

    10/21

    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.

    4-The Built-In Conversion Functions

  • 8/9/2019 Week-4a Built in Functions

    11/21

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

    SELECT 23||2 FROM DUAL

  • 8/9/2019 Week-4a Built in Functions

    12/21

    Name Description

    DECODE Decode compares expression to the search_x expressions and, if matches, returnsresult_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_1WHEN condition_2 THEN result_2...WHEN condition_n THEN result_nELSE result

    END

    USER USER function returns the user_id from the current Oracle session.SELECT USER FROM DUAL; >>>>> SCOTT

    UID UID function returns the id number for a user's session (the user who is currentlylogged in).SELECT UID FROM DUAL; >>>>> 54

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

    The syntax for the coalesce function is:coalesce( expr1, expr2, ... expr_n )

    Other Built-In functions

  • 8/9/2019 Week-4a Built in Functions

    13/21

    1-Write an SQL statement which returns the employees and their salary which added percentage of salary as shownbelow.

    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;

  • 8/9/2019 Week-4a Built in Functions

    14/21

    1-Write an SQL statement which returns the employees and their salary which added percentage of salary as shownbelow.

    30% to MANAGER, 40% to ANALYST,50% to CLERK, 60% to SALESMAN, 70% to PRESIDENTSELECT 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

  • 8/9/2019 Week-4a Built in Functions

    15/21

    SELECT NAME, COALESCE ( ADDRESS1,ADDRESS2,ADDRESS3 ) AS ADDRESS FROM GUIDE

    TABLE: GUIDE

  • 8/9/2019 Week-4a Built in Functions

    16/21

  • 8/9/2019 Week-4a Built in Functions

    17/21

    1-Write an SQL statement which returns

    )30sin() 0a 150log) 10b 1024log) 2c

    )45cos()0

    g

    )1tan()ae)1cos()ad

    )5.0sin()a f

    123

    2 86ln600log45tan70cos30sin

    ) ++++h

    2-Write an SQL statement which returns

    3.56 >>> ? Function>>> 3

    5.286 >>> ? Function>>> 5.29

    7.12 >>> ? Function>>> 77.12 >>> ? Function>>> 8

    5.45612>>>? Function>>> 5.45

    567>>>>>>? Function>>> 570

  • 8/9/2019 Week-4a Built in Functions

    18/21

    Use AIRPLANES table for 3rd ,4 th and 5 th tasks

    3-Write an SQL statement which return ONE if line_number=1,TWO if line_number=2or MORE THAN TWO if line_number is greater than 2.

  • 8/9/2019 Week-4a Built in Functions

    19/21

    4-Write an SQL statement which returns the full name of customer_id as shown in thefigure.

    AAL American AirlinesILC Intl. Leasing Corp.NWO Northwest OrientSAL Southwest AirlinesSWA Sweptwing AirlinesUSAF U.S. Air Force

    5-Write an SQL statement which returnsONE, 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 W i SQL hi h l i l i d l1 l2 l3

  • 8/9/2019 Week-4a Built in Functions

    20/21

    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

  • 8/9/2019 Week-4a Built in Functions

    21/21

    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;