ppt db25 oracle 01afternoon

14
Basics of SQL Oracle Day 1 Afternoon Session

Upload: zubairpam

Post on 18-Oct-2015

13 views

Category:

Documents


0 download

DESCRIPTION

Oracle Power Point

TRANSCRIPT

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    1/14

    Basics of SQLOracle Day 1 Afternoon Session

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    2/14

    2Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    Objectives

    To understand important SQL functions

    To understand pseudo columns

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    3/14

    3Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    DUAL - The Dummy table

    Automatically created table, which is part of the data dictionary

    Contains one row and one column(varchar2(1): value = X)

    Can be used to return constants once, with a SELECT statement

    Belongs to SYS schema, accessible to all

    eg: SELECT SYSDATE FROM DUAL

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    4/14

    4Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    SQL Functions - Decode

    Decode

    DECODE( expr , search , result [, search , result]... [, default] )

    A DECODE function compares exprto each search value one by one. If expris

    equal to a search, Oracle returns the corresponding result. If no match is found,

    Oracle returns default, or, if defaultis omitted, returns null.

    This example decodes the value warehouse_id. If warehouse_id is 1, the function

    returns 'Southlake'; if warehouse_id is 2, it returns 'San Francisco'; etc. If

    warehouse_id is not 1, 2, 3, or 4, the function returns 'Non-domestic'.

    SELECT product_id, DECODE (warehouse_id, 1, 'Southlake', 2, 'San Francisco', 3,

    'New Jersey', 4, 'Seattle', 'Non-domestic') quantity_on_hand FROM inventories;

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    5/14

    5Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    SQL Functions - Trim

    Trim

    TRIM enables you to trim leading or trailing characters (or both) from a character

    string. If trim_characteror trim_sourceis a character literal, you must enclose it in

    single quotes.

    SELECT TRIM (0 FROM 0009872348900) "TRIM Example" FROM DUAL;

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    6/14

    6Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    SQL Functions - Substr

    substr

    The substring functions return a portion of string, beginning at characterposition,

    substring_lengthcharacters long. SUBSTR calculates lengths using characters as

    defined by the input character set. SUBSTRB uses bytes instead of characters.

    SELECT SUBSTR('ABCDEFG',3,4) "Substring" FROM DUAL;

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    7/147Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    SQL Functions - NVL

    NVL

    NVL( expr1 , expr2 )

    If expr1is null, NVL returns expr2. If expr1is not null, NVL returns expr1.

    The following example returns a list of employee names and commissions,

    substituting "Not Applicable" if the employee receives no commission:

    SELECT last_name, NVL(TO_CHAR(commission_pct), 'Not Applicable')

    "COMMISSION" FROM employees WHERE last_name LIKE 'B%';

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    8/148Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    SQL Functions - Translate

    Translate

    TRANSLATE( 'char' , 'from_string' , 'to_string' )

    TRANSLATE returns charwith all occurrences of each character in from_string

    replaced by its corresponding character in to_string. Characters in charthat are not

    in from_stringare not replaced.

    The following statement translates a license number. All letters 'ABC...Z' aretranslated to 'X' and all digits '012 . . . 9' are translated to '9':

    SELECT TRANSLATE('2KRW229',

    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',

    '9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "License" FROM DUAL;

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    9/149Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    SQL FunctionsTo_char

    To_char

    TO_CHAR( date , fmt )

    TO_CHAR converts dateof DATE to a value of VARCHAR2 datatype in the format

    specified by the date format fmt.

    SELECT TO_CHAR(ts_col, 'DD-MON-YYYY HH24:MI:SS') FROM my_tab;

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    10/1410Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    Pseudocolumns

    Behaves like a table column

    Not stored in table Cannot change value of pseudocolumn

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    11/1411Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    Pseudocolumns

    CURRVAL

    NEXTVAL

    ROWID

    ROWNUM

    LEVEL(used for hierarchical queries)

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    12/1412Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    Examples

    SELECT empSeq.CURRVAL from DUAL;

    SELECT ROWID, ENAME FROM EMP; SELECT ROWNUM, ENAME FROM EMP order by ename;

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    13/1413Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No. 2.0

    Summary

    To understand important SQL functions

    To understand pseudo columns

  • 5/28/2018 PPT DB25 Oracle 01Afternoon

    14/1414Copyright 2005, Infosys

    Technologies Ltd

    ER/CORP/CRS/DB25/003

    Version No 2 0

    Thank You!