Top Banner

of 44

e computer notes - Single Row Functions

Apr 06, 2018

Download

Documents

ecomputernotes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/3/2019 e computer notes - Single Row Functions

    1/44

    Single-Row Functions

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    2/44

    Case Manipulation Functions

    These functions convert case for character strings.

    Function Result

    LOWER('SQL Course') sql course

    UPPER('SQL Course') SQL COURSE

    INITCAP('SQL Course') Sql Course

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    3/44

    Using Case Manipulation Functions

    Display the employee number, name, and department

    number for employee Higgins:

    SELECT employee_id, last_name, department_idFROM employees

    WHERE last_name = 'higgins';no rows selected

    SELECT employee_id, last_name, department_id

    FROM employees

    WHERE LOWER(last_name) = 'higgins';

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    4/44

    Character-Manipulation Functions

    These functions manipulate character strings:

    Function Result

    CONCAT('Hello', 'World') HelloWorld

    SUBSTR('HelloWorld',1,5) Hello

    LENGTH('HelloWorld') 10

    INSTR('HelloWorld', 'W') 6

    LPAD(salary,10,'*') **24000

    RPAD(salary, 10, '*') 24000*****TRIM('H' FROM 'HelloWorld') elloWorld

  • 8/3/2019 e computer notes - Single Row Functions

    5/44

    Using the Character-ManipulationFunctions

    1

    ELECT employee_id, CONCAT(first_name, last_name) NAME,

    job_id, LENGTH (last_name),

    INSTR(last_name, 'a') "Contains 'a'?"

    ROM employees

    WHERE SUBSTR(job_id, 4) = 'REP';

    1 2 3

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    6/44

    Number Functions

    " ROUND: Rounds value to specified decimal

    ROUND(45.926, 2) 45.93

    " TRUNC: Truncates value to specified decimal

    TRUNC(45.926, 2) 45.92

    " MOD: Returns remainder of division

    MOD(1600, 300) 100

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    7/44

    Using theROUNDFunction

    1 2

    SELECT ROUND(45.923,2), ROUND(45.923,0),

    ROUND(45.923,-1) 3FROM DUAL;

    1 2 3

    DUALis a dummy table you can use to view resultsfrom functions and calculations.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    8/44

    Using theTRUNCFunction

    1 2

    SELECT TRUNC(45.923,2), TRUNC(45.923),

    TRUNC(45.923,-2) 3FROM DUAL;

    1 2 3

  • 8/3/2019 e computer notes - Single Row Functions

    9/44

    Using theMODFunction

    Calculate the remainder of a salary after it is dividedby 5000 for all employees whose job title is salesrepresentative.

    SELECT last_name, salary, MOD(salary, 5000)FRO M employees

    WHERE job_id = 'SA_REP';

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    10/44

    Working with Dates

    "Oracle database stores dates in an internalnumeric format: century, year, month, day, hours,minutes, seconds.

    "The default date display format is DD-MON-RR.

    Allows you to store 21st century dates in the 20th

    century by specifying only the last two digits of theyear.

    Allows you to store 20th century dates in the 21stcentury in the same way.

    SELECT last_name, hire_date

    FROM employees

    WHERE last_name like 'G % ';

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    11/44

    Working with Dates

    SYSDATEis a function that returns:

    " Date

    " Time

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    12/44

    Arithmetic with Dates

    "Add or subtract a number to or from a date for aresultant date value.

    "Subtract two dates to find the number of daysbetween those dates.

    "Add hours to a date by dividing the number ofhours by 24.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    13/44

    Using Arithmetic Operatorswith Dates

    SELECT last_name, (SYSDATE-hire_date)/7 AS WEEKS

    FROM employees

    WHERE department_id = 90;

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    14/44

    Date Functions

    Function Description

    MONTHS_BETWEEN Number of monthsbetween two dates

    ADD_MONTHS Add calendar months to

    dateNEXT_DAY Next day of the date

    specified

    LAST_DAY Last day of the month

    ROUND Round date

    TRUNC Truncate date

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    15/44

    Using Date Functions

    MONTHS_BETWEEN ('01-SEP-95','11-JAN-94

    19.677419

    ADD_MONTHS ('11-JAN-94',6) '11-JUL-94

    NEXT_DAY ('01-SEP-95','FRIDAY')

    '08-SEP-95

    LAST_DAY('01-FEB-95') '28-FEB-95

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    16/44

    Using Date Functions

    AssumeSYSDATE = '25-JUL-95':

    ROUND(SYSDATE,'MONTH') 01-AUG-95

    ROUND(SYSDATE ,'YEAR') 01-JAN-96

    TRUNC(SYSDATE ,'MONTH') 01-JUL-95

    TRUNC(SYSDATE ,'YEAR') 01-JAN-95

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    17/44

    Implicit Data Type Conversion

    For assignments, the Oracle server can automaticallyconvert the following:

    From To

    VARCHAR2 or CHAR NUMBER

    VARCHAR2 or CHAR DATE

    NUMBER VARCHAR2

    DATE VARCHAR2

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    18/44

    Implicit Data Type Conversion

    For expression evaluation, the Oracle Server canautomatically convert the following:

    From To

    VARCHAR2 or CHAR NUMBER

    VARCHAR2 or CHAR DATE

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    19/44

    Explicit Data Type Conversion

    TO_NUMBER TO_DATE

    NUMBERCHARACTER DATE

    TO_CHARTO_CHAR

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    20/44

    Explicit Data Type Conversion

    TO_NUMBE TO_DATE

    R

    CHARACTER DATE

    NUMBER

    TO_CHAR

    TO_CHAR

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    21/44

    Using theTO_CHARFunction with Dates

    TO_CHAR(date,''format_model')')

    The format model:

    " Must be enclosed in single quotation marks and is cassensitive

    " Can include any valid date format element

    " Has anfmelement to remove padded blanks orsuppress leading zeros

    " Is separated from the date value by a comma

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    22/44

    Elements of the Date Format Model

    YYYY Full year in numbers

    YEAR Year spelled out

    M M Two-digit value for month

    MONTH Full name of the month

    Three-letter abbreviation of theMON

    month

    Three-letter abbreviation of theDY

    day of the week

    DAY Full name of the day of the week

    DD Numeric day of the month

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    23/44

    Elements of the Date Format Model

    " Time elements format the time portion of the date.

    HH 24:MI:SS AM 15:45:32 PM

    " Add character strings by enclosing them in double

    quotation marks.

    DD "of" MONTH 12 of OCTOBER

    " Number suffixes spell out numbers.

    ddspth fourteenth

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    24/44

    Using theTO_CHARFunction with Dates

    ELECT last_name,

    TO_CHAR(hire_date, 'fmDD Month YYYY')AS HIREDATE

    ROM employees;

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    25/44

    Using theTO_CHARFunction withNumbers

    TO_CHAR(number,''format_model')')

    These are some of the format elements you can usewith theTO_CHARfunction to display a number valueas a character:

    9 Represents a number

    0 Forces a zero to be displayed

    $ Places a floating dollar sign

    L Uses the floating local currency symbol

    Prints a decimal point

    , Prints a thousand indicator

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    26/44

    Using theTO_CHARFunction with Numbers

    SELECT TO_CHAR(salary, '$99,999.00') SALARY

    FROM employees

    WHERE last_name = 'Ernst';

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 e computer notes - Single Row Functions

    27/44

    Using theTO_NUMBERandTO_DATEFunctions

    "Convert a character string to a number formatusing theTO_NUMBERfunction:

    TO_NUMBER(char[[,,''format_model'])'])

    "Convert a character string to a date format using

    theTO_DATEfunction:

    TO_DATE(char[, '[, 'format_model'])'])

    "These functions have an fx modifier. This modifier

    specifies the exact matching for the character

    argument and date format model of aTO_DATEfunction

  • 8/3/2019 e computer notes - Single Row Functions

    28/44

    Using theTO_NUMBERandTO_DATEFunctions

    "Convert a character string to a number formatusing theTO_NUMBERfunction:

    TO _NUM BER(char[[,,''format_model'])'])

    "Convert a character string to a date format using

    theTO_DATEfunction:

    TO_DATE(char[, '[, 'format_model'])'])

    "These functions have an fx modifier. This modifier

    specifies the exact matching for the character

    argument and date format model of aTO_DATEfunction

  • 8/3/2019 e computer notes - Single Row Functions

    29/44

    RR Date Format

    Current Year Specified Date RR Format YY Format

    1995 27-OCT-95 1995 1995

    1995 27-OCT-17 2017 1917

    2001 27-OCT-17 2017 2017

    2001 27-OCT-95 1995 2095

    If the specified two-digit year is:

    049 5099

    If two digits The return date is inThe return date is inof the 049 the century beforethe current centurycurrent the current one

    year are: The return date is in The return date is in5099 the century after the current century

    the current one

  • 8/3/2019 e computer notes - Single Row Functions

    30/44

    Example ofRRDate Format

    To find employees hired prior to 1990, use theRRformat, which produces the same results whether the

    command is run in 1999 or now:

    SELECT last_name, TO_CHAR(hire_date, 'DD-Mon-YYYY')

    FROM employeesWHERE hire_date < TO_DATE('01-Jan-90', 'DD-Mon-RR');

  • 8/3/2019 e computer notes - Single Row Functions

    31/44

    Nesting Functions

    " Single-row functions can be nested to any level.

    " Nested functions are evaluated from deepest levelto the least deep level.

    F3(F2(F1(col,arg1),arg2),arg3)

    Step 1 = Result 1

    Step 2 = Result 2Step 3 = Result 3

  • 8/3/2019 e computer notes - Single Row Functions

    32/44

    Nesting Functions

    SELECT last_name,

    NVL(TO_CHAR(manager_id), 'No Manager')FROM employees

    WHERE manager_id IS NULL;

  • 8/3/2019 e computer notes - Single Row Functions

    33/44

    General Functions

    These functions work with any data type and pertainto using nulls.

    " NVL (expr1, expr2)

    " NVL2 (expr1, expr2, expr3)

    " NULLIF (expr1, expr2)

    " COALESCE (expr1, expr2, ..., exprn)

  • 8/3/2019 e computer notes - Single Row Functions

    34/44

    NVLFunction

    Converts a null to an actual value.

    "Data types that can be used are date, character,and number.

    "Data types must match:

    NVL(commission_pct,0)

    NVL(hire_date,'01-JAN-97')

    NVL(job_id,'No Job Yet')

  • 8/3/2019 e computer notes - Single Row Functions

    35/44

    Using theNVLFunction

    1SELECT last_name, salary, NVL(commission_pct, 0),

    (salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SALFROM employees;

    1 2

  • 8/3/2019 e computer notes - Single Row Functions

    36/44

    Using theNVL2Function

    1SELECT last_name, salary, commission_pct,

    NVL2(commission_pct, 2'SAL+COMM', 'SAL') income

    FROM employees WHERE department_id IN (50, 80);

    1 2

  • 8/3/2019 e computer notes - Single Row Functions

    37/44

    Using theNULLIFFunction

    1

    SELECT first_name, LENGTH(first_name) "expr1", 2last_name, LENGTH(last_name) "expr2",NULLIF(LENGTH(first_name), LENGTH(last_name)) result

    FROM employees;

    1 2 3

  • 8/3/2019 e computer notes - Single Row Functions

    38/44

    Using theCOALESCEFunction

    "The advantage of the COALESCE function over theNVLfunction is that theCOALESCEfunction cantake multiple alternate values.

    "If the first expression is not null, it returns that

    expression; otherwise, it does aCOALESCEof theremaining expressions.

  • 8/3/2019 e computer notes - Single Row Functions

    39/44

    Using theCOALESCEFunction

    SELECT last_name,

    COALESCE(commission_pct, salary, 10) commFROM employees

    ORDER BY commission_pct;

  • 8/3/2019 e computer notes - Single Row Functions

    40/44

    Conditional Expressions

    "Provide the use of IF-THEN-ELSE logic within aSQL statement

    " Use two methods:

    CASEexpression

    DECODEfunction

  • 8/3/2019 e computer notes - Single Row Functions

    41/44

    TheCASEExpression

    Facilitates conditional inquiries by doing the work ofan IF-THEN-ELSE statement:

    CASEexpr WHENcomparison_expr1 THENreturn_expr1

    [WHENcomparison_expr2 THENreturn_expr2

    WHENcomparison_exprn THENreturn_exprn]

    ELSEelse_expr]END

  • 8/3/2019 e computer notes - Single Row Functions

    42/44

    Using theCASEExpression

    Facilitates conditional inquiries by doing the work ofan IF-THEN-ELSE statement:SELECT last_name, job_id, salary,

    CASE job_id WHEN 'IT_PROG' THEN 1.10*salary

    WHEN 'ST_CLERK' THEN 1.15*salary

    WHEN 'SA_REP' THEN 1.20*salary ELSE salary END"REVISED_SALARY" FROM employees;

  • 8/3/2019 e computer notes - Single Row Functions

    43/44

    TheDECODEFunction

    Facilitates conditional inquiries by doing the work ofaCASEor IF-THEN-ELSE statement:

    DECODE(col|expression, search1, result1[ ]

    [, search2, result2,...,][ ])[, default])

  • 8/3/2019 e computer notes - Single Row Functions

    44/44

    Using theDECODEFunction

    SELECT last_name, job_id, salary,

    DECODE(job_id, 'IT_PROG', 1.10*salary,'ST_CLERK', 1.15*salary,

    'SA_REP', 1.20*salary,salary)

    REVISED_SALARY

    FROM employees;