Sunday, April 02, 2006
Logical Operators & Single-row functions
Comparison operators:
WHERE column_name='string';
= | > | >= | < | <= | <> |
Other Comparison operators:
BETWEEN ... AND .... | IN(set) | LIKE | IS NULL |
WHERE Salary BETWEEN 2500 AND 3500; | WHERE cust_id IN(101, 102, 105); | WHERE job LIKE '_sal%'; _ : single character % : zero or more | WHERE mgr_id IS NULL; |
Escape Characters to search wildcard characters : WHERE job_id LIKE '%S\_%' ESCAPE '\';
Logical Conditions :
AND | OR | NOT |
WHERE job LIKE '_sal%' | WHERE job LIKE '_sal%' | WHERE cust_id NOT IN(101, 102, 105); |
Rules of Precedence:
Order Evaluated | Operator |
---|---|
1 | Arithmatic operators |
2 | Concatenation operator |
3 | Comparison conditions |
4 | IS [NOT] NULL, LIKE, [NOT] IN |
5 | [NOT] BETWEEN |
6 | NOT logical condition |
7 | AND logical condition |
8 | OR logical condition |
Single Row Functions
Character Functions:
LOWER(column|expression)
UPPER(column|expression)
INITCAP(column|expression)
CONCAT(column1|expression1, column2|expression2)
SUBSTR(column|expression,m [, n])
LENGTH(column|expression)
INSTR(column|expression,'String' [,m] [, n])
LPAD(column|expression, n, 'String')
PPAD(column|expression, n, 'String')
TRIM(leading|trailing|both,trim_char FROM trim_source)
REPLACE(text, search_string,replacement_string)
Number functions:
ROUND(column|expression, n)
TRUNC(column|expression, n)
MOD(m,n)
Date functions:
| : | (+ve if date1>date2) |
| : | N= number of calender months |
| : | 'char' : a number representing the day of week, or a char string. |
| : | Last date of the month containing date |
| : | If fmt omitted, date rounded to nearest day |
| : | If fmt omitted, date truncated to nearest day |
date+no_of_days=date
date-no_of_days=date
date-date=no of days
SYSDATE : system date
Implicit Data Type Conversion :
For assignments: | For expression evaluation: |
VARCHAR2 OR CHAR-> NUMBER | VARCHAR2 OR CHAR-> NUMBER |
Explicit Data Type Conversion :
TO_CHAR(number|date,[fmt],[nlsparams]) | : | Convert a number of date value to VARCHAR2 as per fmt. nlsparams specifies decimal character, group separater, local currency symbol & international currency symbol. If nlsparams or any other parameter is omitted, default session parameter values are used. |
TO_DATE(char,[fmt],[nlsparams]) | : | If fmt is omitted DD-MON-YY is used. |
Date format Model :
| : | Full year in numbers |
| : | Year spelled out |
| : | Two digit value of month |
| : | Full name of the month |
| : | Three letter abbreviation of month |
| : | Three letter abbreviation of day-of-week |
| : | Full name of week |
| : | Numeric day of month |
Other formats & number displays :
| : : : : : | Punctuation reproduced in the result quoted string reproduced in the result Ordinal Number (eg. DDTH for 4TH) spelled out number (eg. DDSP for FOUR) spelled out ordinal number (eg. DDSPTH for FOURTH) |
Number format Model :
Element | Desc | Example | Result |
9 | Numeric position | 999999 | 1234 |
0 | Leading zeros | 099999 | 001234 |
$ | Floating dollar sign | $999999 | $1234 |
L | Floating local currency symbol | L999999 | FF1234 |
. | Decimal point in position specified | 999999.99 | 1234.00 |
, | Comma in position specified | 999,999 | 1,234 |
MI | Minus sign to right (-ve values) | 999999MI | 1234- |
PR | Parenthesize -ve numbers | 999999PR | <1234> |
EEEE | Scientific notation | 99.999EEEE | 1.234E+03 |
V | Multiply by 10 n times (n=no. of 9s after V) | 9999V99 | 123400 |
B | Zero value as blank, no zero. | B9999.99 | 1234.00 |
Eg. :
TO_CHAR(salary, '$99,999.99')
TO_DATE('May 24, 1999', 'fxMonth DD, YYYY');
Note : “fx” modifier specifies exact match. Any extra space will cause the conversion to fail. Without “fx” Oracle ignores extra blanks.
General Functions
NVL (expr1, expr2) | : | Converts a Null value to an actual value example: NVL(commission_pct,0); NVL conversion:
| |||||||||
NVL2 (expr1, expr2, expr3) | | If expr1 <> NULL returns expr2 example: NVL2(commission_pct,'SAL+COMM','SAL'); Note :
| |||||||||
NULLIF (expr1, expr2) | : | If expr1=expr2 , returns NULL else return expr1 | |||||||||
COALESCE (expr1, expr2,..., exprn) | : | Returns the 1st non-null expression in the expression list. Example: COALESCE (commission_pct, salary,0); |
Conditional Expressions:
CASE expr WHEN comparison_expr1 THEN return_expr1
| [WHEN comparison_expr2 THEN return_expr2 |
END
Example:
SELECT last_name, job_id, salary,
| CASE job_id WHEN 'SAL_CLK' THEN salary * 1.10
END “REVISED_SALARY |
FROM employees;
DECODE (col|exprn, search1, result1,
| [search2, result2,....] |
Example:
SELECT last_name, job_id, salary,
| DECODE (job_id, 'SAL_CLK', salary * 1.10,
|
FROM employees;
Advertisements
|
ARCHIVES |
Recent Postings
|