锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
The syntax for the NVL function is:
NVL( string1, replace_with )
string1 is the string to test for a null value.
replace_with is the value returned if string1 is null.
Example #1:
select NVL(supplier_city, 'n/a')
from suppliers;
The SQL statement above would return 'n/a' if the supplier_city field contained a null value. Otherwise, it would return the supplier_city value.
Example #2:
select supplier_id,
NVL(supplier_desc, supplier_name)
from suppliers;
This SQL statement would return the supplier_name field if the supplier_desc contained a null value. Otherwise, it would return the supplier_desc.
Example #3:
select NVL(commission, 0)
from sales;
This SQL statement would return 0 if the commission field contained a null value. Otherwise, it would return the commission field.
Question:聽 I tried to use the NVL function through VB to access Oracle DB.
To be precise,
select NVL(DIstinct (emp_name),'AAA'),................ from.................
I got an oracle error when I use distinct clause with NVL, but when I remove distinct it works fine.
Answer:聽 It is possible to the use the DISTINCT clause with the NVL function. However, the DISTINCT must come before the use of the NVL function. For example:
select distinct NVL(emp_name, 'AAA')
from employees;
Hope this helps!
Question:聽 Is it possible to use the NVL function with more than one column with the same function call?聽 To be clear, if i need to apply this NVL function to more than one column like this:
NVL(column1;column2 ...... , here is the default value for all )
Answer:聽 You will need to make separate NVL function calls for each column. For example:
select NVL(table_name, 'not found'), NVL(owner, 'not found')
from all_tables;
The syntax for the decode function is:
decode( expression , search , result [, search , result]... [, default] )
expression is the value to compare.
search is the value that is compared against expression.
result is the value returned, if expression is equal to search.
default is optional. If no matches are found, the decode will return default. If default is omitted, then the decode statement will return null (if no matches are found).
For Example:
You could use the decode function in an SQL statement as follows:
SELECT supplier_name, decode(supplier_id, 10000, 'IBM', 10001, 'Microsoft', 10002, 'Hewlett Packard', 'Gateway') result FROM suppliers;
The above decode statement is equivalent to the following IF-THEN-ELSE statement:
IF supplier_id = 10000 THEN
聽聽聽聽 result := 'IBM';ELSIF supplier_id = 10001 THEN
聽聽聽 result := 'Microsoft';ELSIF supplier_id = 10002 THEN
聽聽聽 result := 'Hewlett Packard';ELSE
聽聽聽 result := 'Gateway';END IF;
The decode function will compare each supplier_id value, one by one.
Question:聽 One of our viewers wanted to know how to use the decode function to compare two dates (ie: date1 and date2), where if date1 > date2, the decode function should return date2. Otherwise, the decode function should return date1.
Answer:聽 To accomplish this, use the decode function as follows:
decode((date1 - date2) - abs(date1 - date2), 0, date2, date1)
The formula below would equal 0, if date1 is greater than date2:
(date1 - date2) - abs(date1 - date2)
Question:聽 I would like to know if it's possible to use decode for ranges of numbers, ie 1-10 = 'category 1', 11-20 = 'category 2', rather than having to individually decode each number.
Answer: Unfortunately, you can not use the decode for ranges of numbers. However, you can try to create a formula that will evaluate to one number for a given range, and another number for the next range, and so on.
For example:
SELECT supplier_id, decode(trunc ((supplier_id - 1) / 10), 0, 'category 1', 1, 'category 2', 2, 'category 3', 'unknown') result FROM suppliers;
In this example, based on the formula:
trunc ((supplier_id - 1) / 10
The formula will evaluate to 0, if the supplier_id is between 1 and 10.
The formula will evaluate to 1, if the supplier_id is between 11 and 20.
The formula will evaluate to 2, if the supplier_id is between 21 and 30.
and so on...
Question:聽 I need to write a decode statement that will return the following:
If yrs_of_service < 1 then return 0.04
If yrs_of_service >= 1 and < 5 then return 0.04
If yrs_of_service > 5 then return 0.06
How can I do this?
Answer:聽 You will need to create a formula that will evaluate to a single number for each one of your ranges.
For example:
SELECT emp_name, decode(trunc (( yrs_of_service + 3) / 4), 0, 0.04, 1, 0.04, 0.06) as perc_value FROM employees;
Helpful Tip: One of our viewers suggested combining the SIGN function with the DECODE function as follows:
The date example above could be modified as follows:
DECODE(SIGN(date1-date2), 1, date2, date1)
The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses
DECODE(SIGN(actual-target), -1, 'NO Bonus for you', 0,'Just made it', 1, 'Congrats, you are a winner')
The syntax for the to_char function is:
to_char( value, [ format_mask ], [ nls_language ] )
value can either be a number or date that will be converted to a string.
format_mask is optional. This is the format that will be used to convert value to a string.
nls_language is optional. This is the nls language used to convert value to a string.
The following are number examples for the to_char function.
to_char(1210.73, '9999.9') would return '1210.7' to_char(1210.73, '9,999.99') would return '1,210.73' to_char(1210.73, '$9,999.00') would return '$1,210.73' to_char(21, '000099') would return '000021'
The following is a list of valid parameters when the to_char function is used to convert a date to a string. These parameters can be used in many combinations.
Parameter Explanation YEAR Year, spelled out YYYY 4-digit year YYY
YY
YLast 3, 2, or 1 digit(s) of year. IYY
IY
ILast 3, 2, or 1 digit(s) of ISO year. IYYY 4-digit year based on the ISO standard Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1). MM Month (01-12; JAN = 01). MON Abbreviated name of month. MONTH Name of month, padded with blanks to length of 9 characters. RM Roman numeral month (I-XII; JAN = I). WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year. W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh. IW Week of year (1-52 or 1-53) based on the ISO standard. D Day of week (1-7). DAY Name of day. DD Day of month (1-31). DDD Day of year (1-366). DY Abbreviated name of day. J Julian day; the number of days since January 1, 4712 BC. HH Hour of day (1-12). HH12 Hour of day (1-12). HH24 Hour of day (0-23). MI Minute (0-59). SS Second (0-59). SSSSS Seconds past midnight (0-86399). FF Fractional seconds.
The following are date examples for the to_char function.
to_char(sysdate, 'yyyy/mm/dd'); would return '2003/07/09' to_char(sysdate, 'Month DD, YYYY'); would return 'July 09, 2003' to_char(sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003' to_char(sysdate, 'MON DDth, YYYY'); would return 'JUL 09TH, 2003' to_char(sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003' to_char(sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'
You will notice that in some examples, the format_mask parameter begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the examples below.
to_char(sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003' to_char(sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003' to_char(sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'
The zeros have been suppressed so that the day component shows as "9" as opposed to "09".
Question:聽 Why doesn't this sort the day's of the week in order?
select ename, hiredate, to_char((hiredate),'fmDay') "Day"
from emp
order by "Day";
Answer:
The fmDay parameter will return the name of the Day and not the numeric value of the day.
Try the following:
select ename, hiredate, to_char((hiredate),'fmDD') "Day"
from emp
order by "Day";
The syntax for the coalesce function is:
coalesce( expr1, expr2, ... expr_n )
For Example:
You could use the coalesce function in an SQL statement as follows:
SELECT coalesce( address1, address2, address3 ) result
FROM suppliers;
The above coalesce statement is equivalent to the following IF-THEN-ELSE statement:
IF address1 is not null THEN
聽聽聽聽 result := address1;ELSIF address2 is not null THEN
聽聽聽 result := address2;ELSIF address3 is not null THEN
聽聽聽 result := address3;ELSE
聽聽聽 result := null;END IF;
The coalesce function will compare each value, one by one.
ASCII | |
Get The ASCII Value Of A Character | ASCII(<string_or_column>) |
SELECT ASCII('A') FROM dual; SELECT ASCII('Z') FROM dual; SELECT ASCII('a') FROM dual; SELECT ASCII('z') FROM dual; SELECT ASCII(' ') FROM dual; |
|
聽 | |
CASE Related Functions | |
Upper Case | UPPER(<string_or_column>) |
SELECT UPPER('Dan Morgan') FROM dual; | |
Lower Case | LOWER(<string_or_column>) |
SELECT LOWER('Dan Morgan') FROM dual; | |
Initial Letter Upper Case | INITCAP(<string_or_column>) |
SELECT INITCAP('DAN MORGAN') FROM dual; | |
NLS Upper Case | NLS_UPPER(<string_or_column>) |
SELECT NLS_UPPER('Dan Morgan', 'NLS_SORT = XDanish') FROM dual; |
|
NLS Lower Case | NLS_LOWER(<string_or_column>) |
SELECT NLS_LOWER('Dan Morgan', 'NLS_SORT = XFrench') FROM dual; |
|
NLS Initial Letter Upper Case | NLS_INITCAP(<string_or_column>) |
SELECT NLS_INITCAP('DAN MORGAN', 'NLS_SORT = XGerman') FROM dual; |
|
聽 | |
CHR | |
Character | CHR(<ascii_string_or_column>>) |
SELECT(CHR(68) || CHR(65) || CHR(78)) FROM dual; SELECT(CHR(68) || CHR(97) || CHR(110)) FROM dual; |
|
聽 | |
COALESCE | |
Returns the first non-null occurrence | COALESCE(<value>, <value>, <value>, ...) |
CREATE TABLE test ( col1聽 VARCHAR2(1), col2聽 VARCHAR2(1), col3聽 VARCHAR2(1)); INSERT INTO test VALUES (NULL, 'B', 'C'); INSERT INTO test VALUES ('A', NULL, 'C'); INSERT INTO test VALUES (NULL, NULL, 'C'); INSERT INTO test VALUES ('A', 'B', 'C'); SELECT COALESCE(col1, col2, col3) FROM test; |
|
聽 | |
CONCAT | |
Concatenate | CONCAT(<first_string_or_column>>, <second_string_or_column>>) |
SELECT CONCAT('Dan ', 'Morgan') FROM dual; | |
聽 | |
CONVERT | |
Converts From One Character Set To Another |
CONVERT(<character>,<destination_character_set>, <source_character_set>) |
SELECT CONVERT('?????A B C D E','US7ASCII','WE8ISO8859P1') FROM dual; |
|
聽 | |
INSTR | |
See links at page bottom | |
聽 | |
LENGTH | |
String Length | LENGTH(<string_or_column>) |
SELECT LENGTH('Dan Morgan') FROM dual; | |
聽 | |
LPAD | |
Left Pad | LPAD(<string_or_column>, <final_length>, <padding_character>) |
SELECT LPAD('Dan Morgan', 25, 'x') FROM dual; | |
聽 | |
LTRIM | |
Left Trim | LTRIM(<string_or_column>) |
SELECT LTRIM('聽聽 Dan Morgan聽聽 ') FROM dual; | |
聽 | |
NLSSORT | |
Returns the string of bytes used to sort a string. The string returned is of RAW data type |
NLSSORT(<column_name>, 'NLS_SORT = <NLS Parameter>); |
CREATE TABLE test (name VARCHAR2(15)); INSERT INTO test VALUES ('Gaardiner'); INSERT INTO test VALUES ('Gaberd'); INSERT INTO test VALUES ('G閳坋rd'); COMMIT; SELECT * FROM test ORDER BY name; SELECT * FROM test ORDER BY NLSSORT(name, 'NLS_SORT = XDanish'); |
|
聽 | |
REPLACE | |
See links at page bottom | |
聽 | |
REVERSE | |
Reverse | REVERSE(<string_or_column>) |
SELECT REVERSE('Dan Morgan') FROM dual; SELECT DUMP('Dan Morgan') FROM dual; SELECT DUMP(REVERSE('Dan Morgan')) FROM dual; |
|
聽 | |
RPAD | |
Right Pad | RPAD(<string_or_column>, <final_length>, <padding_character>) |
SELECT RPAD('Dan Morgan', 25, 'x') FROM dual; | |
聽 | |
RTRIM | |
Right Trim | RTRIM(<string_or_column>) |
SELECT RTRIM('聽聽 Dan Morgan聽聽 ') FROM dual; | |
聽 | |
SOUNDEX | |
Returns Character String Constaining The Phonetic Representation Of Another String |
Rules:
SOUNDEX(<string_or_column>) |
CREATE TABLE test ( name VARCHAR2(15)); INSERT INTO test VALUES ('Smith'); INSERT INTO test VALUES ('Smyth'); INSERT INTO test VALUES ('Smythe'); INSERT INTO test VALUES ('Smither'); INSERT INTO test VALUES ('Smidt'); INSERT INTO test VALUES ('Smick'); INSERT INTO test VALUES ('Smiff'); COMMIT; SELECT * FROM test; SELECT * FROM test WHERE SOUNDEX(name) = SOUNDEX('SMITH'); |
|
聽 | |
SUBSTR | |
See links at page bottom | |
聽 | |
TRANSLATE | |
See links at page bottom | |
聽 | |
TREAT | |
Changes The Declared Type Of An Expression | TREAT (<expression> AS REF schema.type))聽 |
SELECT name, TREAT(VALUE(p) AS employee_t).salary SALARY聽 FROM persons p; |
|
聽 | |
TRIM (variations are LTRIM and RTRIM) | |
Trim Spaces | TRIM(<string_or_column>) |
SELECT '聽聽 Dan Morgan 聽聽 ' FROM dual; SELECT TRIM('聽聽 Dan Morgan聽聽 ') FROM dual; |
|
Trim Other Characters | TRIM(<character_to_trim> FROM <string_or_column>) |
SELECT TRIM('D' FROM 'Dan Morgan') FROM dual; | |
Trim By CHR value | TRIM(<string_or_column>) |
SELECT ASCII(SUBSTR('Dan Morgan',1,1)) FROM dual; SELECT TRIM(CHR(68) FROM 'Dan Morgan') FROM dual; |
|
聽 | |
Vertical Bars | |
Also known as Pipes | <first_string> || <second_string> |
SELECT 'Dan' || ' ' || 'Morgan' FROM dual; with alias SELECT 'Dan' || ' ' || 'Morgan' NAME FROM dual; or SELECT 'Dan' || ' ' || 'Morgan' AS NAME FROM dual; |
|
聽 | |
VSIZE | |
Byte Size | VSIZE(<string_or_column>) |
SELECT VSIZE('Dan Morgan') FROM dual; |
Related Topics |
CASE |
DBMS_LOB |
Decode |
Instring |
Miscellaneous Functions |
Operators (Built-in) |
Regular Expressions |
Replace |
Substring |
Translate |
XML Functions |
The syntax for the trim function is:
trim( [ leading | trailing | both聽 [ trim_character ] 聽]聽聽 string1 )
leading - remove trim_string from the front of string1.
trailing - remove trim_string from the end of string1.
both - remove trim_string from the front and end of string1.
If none of these are chosen (ie: leading, trailing, both), the trim function will remove trim_string from both the front and end of string1.
trim_character is the character that will be removed from string1. If this parameter is omitted, the trim function will remove all leading and trailing spaces from string1.
string1 is the string to trim.
For example:
trim('聽聽 tech聽聽 ') would return 'tech' trim(' '聽 from聽 '聽聽 tech聽聽 ') would return 'tech' trim(leading '0' from '000123') would return '123' trim(trailing '1' from 'Tech1') would return 'Tech' trim(both '1' from '123Tech111') would return '23Tech'