Vous êtes sur la page 1sur 9

COMPARISION CONDITION

OPERATOR = > < >= <= <>,^=,!= BETWEEN ..AND MEANING EQUAL TO GREATER THAN LESS THAN GREATER THAN EQUAL TO LESS THAN EQUAL TO NOT EQUAL TO BETWEEN TWO VALUE(INCLUSIVE) (This function can be used in both either in character or numeric and date type) MATCH THE ANY OF THE LIST OF VALUE MATCH THE CHARACTER PATTERN

IN(Set) For e.g.: IN(100,101) e.g.: IN(ROSHAN,RAMESH) LIKE For e.g. first_name LIKE S%;(shows name starting from S) S_ show all with SS,SA,SB etc IS NULL NOT IN ( , )

IS A NULL VALUE RETURNS OTHER VALUE THAN GIVEN IN NOT IN CONDITION. NOT can be used with BETWEEN, IN, LIKE function also. IMP: SELECT employee_id, first_name,last_name,job_id FROM employees WHERE job_id LIKE %SA\_% ESCAPE \;
The artists name is retrieved whenever the artist is found in the GUESTAPPEARANCE table: SELECT NAME FROM ARTIST WHERE EXISTS (SELECT GUESTARTIST_ID FROM GUESTAPPEARANCE); SELECT A.NAME FROM ARTIST A WHERE EXISTS (SELECT GUESTARTIST_ID FROM GUESTAPPEARANCE WHERE GUESTARTIST_ID = A.ARTIST_ID); SELECT NAME FROM ARTIST WHERE ARTIST_ID BETWEEN 1 AND 10; SELECT NAME FROM ARTIST WHERE ARTIST_ID = ANY (SELECT GUESTARTIST_ID FROM GUESTAPPEARANCE); SELECT RECORDING_DATE, PLAYING_TIME, TITLE FROM SONG WHERE TITLE BETWEEN 'A' and 'C' ORDER BY PLAYING_TIME NULLS FIRST;

Pseudocolumns
ROWID, ROWNUM, <sequence>.CURRVAL, <sequence>.NEXTVAL, LEVEL

SQLPLUS
CMDS[EP] =command separator
SET 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 a new 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 out a 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 file
EDIT 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=9 COLUMN 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 NODUPLICATES COMPUTE COUNT OF SONGTITLE ON CDTITLE

CHARACTER MANIPULATION FUNCTION

FUNCTION CONCAT(HELLO,WORLD) or HELLO||WORLD SUBSTR(HELLOWORLD,1,5) LENGTH(HELLOWORLD) INSTR(HELLOWORLD,W) LPAD(SALARY,10,*) RPAD(SALARY,10,*) REPLACE(JACK and JUE,J,BL) TRIM(H from HELLOWORLD) LOWER(expression) UPPER(expression) INITCAP(expression) INSTR(expression, substring [, position [, occurrence]]) TRIM([[LEADING|TRAILING|BOTH] character FROM] expression),

OUTPUT HELLOWORLD HELLO 10 6 *****24000 24000***** BLACK AND BLUE ELLOWORLD

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

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 ROUND (49.926,2) TRUNC(49.926,2) MOD(1600, 300) 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). OUTPUT 45.93 (Round the value to specified decimal) 45.92 (Truncates the value to specified decimal) 100 (Return Reminder of the Division)

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 Date+Number Date-Number Date-Date Date+Number/24 RESULT Date Date Number of Days Date DESCRIPTION Add a number of days to date Substract a number of days from date Substract one date from another Add a number of hours to date

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

ADD_MONTHS(date ,n) NEXT_DAY(date,charachter) LAST_DAY(date) ROUND TRUNC

ADD MONTH TO DATE NEXT DAYS AS SPECIFIED BY US LAST DAY OF MONTH ROUND DATE TRUNC DATE

Format 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 ROUND(SYSDATE,MONTH) ROUND(SYSDATE,YEAR) TRUNC(SYSDATE,MONTH) TRUNC(SYSDATE,YEAR) 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. RESULT 01-AUG-03 01-JAN-04 01-JUL0-03 01-JAN-04

DECODE ,NVL, NVL2


FUNCTION DECODE(E1,E2,E3,E4,E5,E6,,En) NVL(E1,E2) NVL2(E1,E2,E3) NULLIF( , ) LOGIC EQUIVALENT IF E1=E2 than E3,E4,E5,., else En IF E1 is NULL than E2 else E1 IF E1 is NULL than E3 else E2 Compare two expression and return NULL if they are equal, returns the first value if they are not equal 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)

COALESCE(E1,E2En, SUBSTITUTION)

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

LEAST(expression [, expression ...]) NULLIF(expression, expression). VSIZE(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_PROVINCE WHEN 'CA' THEN 'Surfer' WHEN 'NH' THEN 'Snow bunny' WHEN 'OR' THEN 'Tree hugger' WHEN 'FL' THEN 'Retired' ELSE 'Whatever!' END FROM ARTIST;

eturns a null value when both expressions are equal The number of bytes in an expression

CASE: C=Condition R=Result CASE WHEN C1 THEN R1 WHEN C2 THEN R2 ... WHEN CN THEN RN ELSE RD END 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

^ $ [ ] ( ) { i }, and { i, j

Start of line. End of line. List of elements allowing match of any expression contained within. Expression parentheses.
{ i, }, }

i matches exactly, at least i matches, and at least i matches but <= j.

COLUMN TITLE FORMAT A32 SELECT TITLE, VAL FROM(SELECT TITLE ,REGEXP_INSTR(title, 'A(/*)') AS VAL FROM SONG ) WHERE VAL > 0;

Vous aimerez peut-être aussi