SQL Detail

Embed Size (px)

Citation preview

  • 8/3/2019 SQL Detail

    1/9

    COMPARISION CONDITION

    OPERATOR MEANING

    = EQUAL TO

    > GREATER THAN

    < LESS THAN>= GREATER THAN EQUAL TO

  • 8/3/2019 SQL Detail

    2/9

    PseudocolumnsROWID, ROWNUM, .CURRVAL, .NEXTVAL, LEVEL

    SQLPLUS

    CMDS[EP] =command separatorSET CMDSEP ~COL NAME HEADING "Artist" ~ COL CITY HEADING "Location"

    COLSEP - Sets the character used between columns in a report.

    ECHO - Tells SQL*Plus to either list the command before executing it (ON)

    ESC[APE] - An escape character allows a command-level character to be used without executing its

    inherent command.

    FEED[BACK] - Determines whether to display feedback (ON), suppress feedback (OFF),

    HEAD[ING] - Set to OFF for no column headings. Set to ON (the default) to display column headings.

    LINE[SIZE] - The number of characters on one line before SQL*Plus starts a new line. The default in

    SQL*Plus is 80

    LONG - Set the default number of bytes retrieved.

    MARK[UP] HTML- Vaguely, this option can be used to generate HTML script from SQL*Plus output.

    NEWP[AGE] Sets the number of blank lines to print before printing the title (if any) and headings on anew page.

    NULL - Sets the string displayed when a null value is returned in a report. The default is a blank space.

    NUMF[ORMAT] { format } Apply a format to all output numbers.For instance, SET NUMFORMAT

    '999,999,999,990.00'

    NUMW[IDTH] - Reset the default of 10 for display width of numbers.

    PAGES[IZE] Sets the number of lines per report page. This is most important when you are printing outa report. The LINESIZE and PAGESIZE must be set correctly to match the printable area. SET PAGES 0

    to suppress formatting.

    PAU[SE] - SET PAUSE ON will wait for the user to press Enter before issuing a page break and

    displaying the next screen.

    RECSEP - Can be set to WRAPPED to output a record separator when a line wraps. Set to OFF to

    disable and EACH to include a record separator for every line.RECSEPCHAR - This option allows resetting of the record separator.

    SQLP[ROMPT] - This option changes the SQL prompt.

    TERM[OUT] - Turns on or off the screen display

    TIMI[NG] - Turns on or off the display of elapsed time.WRAP - Word or line wrapping implies that text overflowing

    SET FEEDBACK OFF such that the text rows selected.UNDEFINE CDNUM or DEFINE CDNUM=2 set variable in sql query.

    SAVE CDREPORT Use the SAVE command to write the SQL buffer contents to a fileEDIT CDREPORT

    @@. This tells SQL*Plus to run the script and, in addition, to look for any scripts called within this script

    in the same directory.

    Formatting query output.DEFINE CDNUM=9COLUMN CDTITLE FORMAT A15 WRAP

    COLUMN NAME FORMAT A12 TRUNCATE HEADING "Artist Name"

    COLUMN SONGTITLE HEADING "Song|Title" FORMAT A20 WORD_WRAP

    BREAK ON CDTITLE NODUPLICATES SKIP 2 ON NAME NODUPLICATESCOMPUTE COUNT OF SONGTITLE ON CDTITLE

  • 8/3/2019 SQL Detail

    3/9

    CHARACTER MANIPULATION FUNCTION

  • 8/3/2019 SQL Detail

    4/9

  • 8/3/2019 SQL Detail

    5/9

    FUNCTION OUTPUT

    CONCAT(HELLO,WORLD) or HELLO||WORLD HELLOWORLD

    SUBSTR(HELLOWORLD,1,5) HELLO

    LENGTH(HELLOWORLD) 10

    INSTR(HELLOWORLD,W) 6

    LPAD(SALARY,10,*) *****24000

    RPAD(SALARY,10,*) 24000*****

    REPLACE(JACK and JUE,J,BL) BLACK AND BLUE

    TRIM(H from HELLOWORLD) ELLOWORLD

    LOWER(expression) UPPER(expression)

    INITCAP(expression)

    INSTR(expression, substring [, position [,

    occurrence]])TRIM([[LEADING|TRAILING|BOTH]character FROM] expression),

    ORDER BY (Clouse)

    Two types: 1.ASC Ascending order (By Default)

  • 8/3/2019 SQL Detail

    6/9

    2. DESC descending order.

    SELECT .

    FROM ..

    ORDER BY hire_date DESC;

    SQL FUNCTION: 1. SINGLE ROW FUNCTION (Return one result per row)

    2. MULTI-ROW FUNCTION (Return one result per set of row)

    NUMBER FUNCTION

    FUNCTION OUTPUT

    ROUND (49.926,2) 45.93 (Round the value to specified decimal)

    TRUNC(49.926,2) 45.92 (Truncates the value to specified decimal)

    MOD(1600, 300) 100 (Return Reminder of the Division)

    ABS(n), CEIL(n) and FLOOR(n).

    POWER(m, n). SIGN(n). SQRT(n)TO_BINARY_DOUBLE(expression, format)and TO_BINARY_

    FLOAT(expression, format)

    NANVL(value, replace). NANVL returns a replacement value if the

    initial value is not a number.

    REMAINDER(n, m).

    For example: SELECT ROUND(45.923,2), ROUND(45.923,0), ROUND(45.923,-1)

    FROM DUAL;

    OUTPUT: 45.92 46 50

    For example: SELECT TRUNC(45.923,2), TRUNC(45.923,0), TRUNC(45.923,-1)

    FROM DUAL;

    OUTPUT: 45.92 45 40

    ARITHMETIC WITH DATE

    OPERATION RESULT DESCRIPTION

    Date+Number Date Add a number of days to date

    Date-Number Date Substract a number of days from

    date

    Date-Date Number of Days Substract one date from

    anotherDate+Number/24 Date Add a number of hours to date

    DATE FUNCTION

    FUNCTION OUTPUT

    MONTHS _BETWEEN(date1,date2) NO. OF MONTHS BETWEEN TWO DATES

  • 8/3/2019 SQL Detail

    7/9

    ADD_MONTHS(date ,n) ADD MONTH TO DATE

    NEXT_DAY(date,charachter) NEXT DAYS AS SPECIFIED BY US

    LAST_DAY(date) LAST DAY OF MONTH

    ROUND ROUND DATE

    TRUNC TRUNC DATEFormat Characters Rounding and Truncating

    CC The first year in a century.

    YYYY, YEAR, YY The nearest year, rounds up on July 1st.

    Q The nearest quarter, rounds up on the 16th of month two.

    MONTH, MON, MM The nearest month, rounds up on the 16th.

    WW The same day of the week as the first day of the year.

    W The same day of the week as the first day of the month.

    DDD, DD The day.

    DAY, D The first day of the week.

    HH, HH12, HH24 The hour (HH24 is a 24-hour clock).

    MI The minute.

    ASSUME SYSTEM DATE: 25-JUL-03

    FUNCTION RESULTROUND(SYSDATE,MONTH) 01-AUG-03

    ROUND(SYSDATE,YEAR) 01-JAN-04

    TRUNC(SYSDATE,MONTH) 01-JUL0-03

    TRUNC(SYSDATE,YEAR) 01-JAN-04

    USING TO_CHAR function with DATE

    TO_CHAR(date,date format )

    For example: TO_CHAR(hire_date,dd-mm-yyyy)

    USING TO_CHAR function with DATE

    TO_CHAR(salary,$99,999999.00) salary

    USING TO_DATE and TO_NUMBER function.

    DECODE ,NVL, NVL2

    FUNCTION LOGIC EQUIVALENT

    DECODE(E1,E2,E3,E4,E5,E6,,En) IF E1=E2 than E3,E4,E5,., else En

    NVL(E1,E2) IF E1 is NULL than E2 else E1

    NVL2(E1,E2,E3) IF E1 is NULL than E3 else E2

    NULLIF( , ) Compare two expression and return NULL if theyare equal, returns the first value if they are not

    equal

    COALESCE(E1,E2En, SUBSTITUTION) Return the first non-NULL expression from the

    expression list. (if null appear then whatever we

    have given input as substitution it will take the

    same)

    GREATEST(expression[, expression ...]) and

  • 8/3/2019 SQL Detail

    8/9

    LEAST(expression [,

    expression ...])

    NULLIF(expression, expression). eturns a null value when both

    expressions are equal

    VSIZE(expression). The number of bytes in an expression

    SELECT STATE_PROVINCE, DECODE(STATE_PROVINCE,'CA','Surfer','NH','Snow bunny','OR', 'Tree hugger','FL', 'Retired','Whatever!')FROM ARTIST;

    Is equal to

    SELECT STATE_PROVINCE, CASE STATE_PROVINCEWHEN 'CA' THEN 'Surfer'WHEN 'NH' THEN 'Snow bunny'WHEN 'OR' THEN 'Tree hugger'WHEN 'FL' THEN 'Retired'

    ELSE 'Whatever!' ENDFROM ARTIST;

    CASE: C=Condition R=ResultCASE

    WHEN C1 THEN R1

    WHEN C2 THEN R2

    ...

    WHEN CN THEN RN

    ELSE RDEND

    Example:

    SELECT last_name, job_id, salary,

    CASE job_id WHEN it_prog THAN 1.10*salary

    WHEN st_clark THAN 1.20*salary

    WHEN president THAN 4.10*salary

    Else salary

    END Revised Salary

    FROM employees;

    Note: DECODE is used only in oracle while CASE is ANSI CODE function can be used in both.

    Regular Expression Patterns

    Character What Is It?

    * Zero or more.

    . Any character (not null).

    ? Zero or one.

    + One or more.

    | OR

  • 8/3/2019 SQL Detail

    9/9

    ^ Start of line.

    $ End of line.

    [ ] List of elements allowing match of any expression contained within.

    ( ) Expression parentheses.{ i }, { i, },and

    { i, j } i matches exactly, at least i matches, and at least i matches but 0;