Oracle Useful Items
Oracle Useful Items
to_date('2003/07/09',
would return a date value of July 9, 2003.
'yyyy/mm/dd')
to_date('070903', 'MMDDYY') would return a date value of July 9, 2003.
to_date('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002.
Decode
SELECT supplier_name,
decode(supplier_id, 10000, 'IBM',
10001, 'Microsoft',
10002, 'Hewlett Packard',
'Gateway') result
FROM suppliers;
ELSE
result := 'Gateway';
END IF;
The decode function will compare each supplier_id value, one by one.
To_Char
For example:
This would create a sequence object called supplier_seq. The first sequence number
that it would use is 1 and each subsequent number would increment by 1 (ie:
2,3,4,...}. It will cache up to 20 values for performance.
If you omit the MAXVALUE option, your sequence will automatically default to:
MAXVALUE 999999999999999999999999999
Now that you've created a sequence object to simulate an autonumber field, we'll
cover how to retrieve a value from this sequence object. To retrieve the next value in
the sequence order, you need to use nextval.
For example:
supplier_seq.nextval
This would retrieve the next value from supplier_seq. The nextval statement needs to
be used in an SQL statement. For example:
This insert statement would insert a new record into the suppliers table. The
supplier_id field would be assigned the next number from the supplier_seq sequence.
The supplier_name field would be set to Kraft Foods.
Frequently Asked Questions
Or you could create the same sequence with the nocache option:
Answer: With respect to a sequence, the cache option specifies how many
sequence values will be stored in memory for faster access.
The downside of creating a sequence with a cache is that if a system failure occurs,
all cached sequence values that have not be used, will be "lost". This results in a
"gap" in the assigned sequence values. When the system comes back up, Oracle will
cache new numbers from where it left off in the sequence, ignoring the so called
"lost" sequence values.
Nocache means that none of the sequence values are stored in memory. This option
may sacrifice some performance, however, you should not encounter a gap in the
assigned sequence values.
Example #1:
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.
In Oracle/PLSQL, the substr functions allows you to extract a substring from a string.
The syntax for the substr function is:
Note:
If start_position is 0, then substr treats start_position as 1 (ie: the first position in the
string).
If start_position is a positive number, then substr starts from the beginning of the
string.
If start_position is a negative number, then substr starts from the end of the string
and counts backwards.
If length is a negative number, then substr will return a NULL value.
For example:
Below is an alphabetical listing of the Oracle system tables that are commonly
used.
System Table Description
SYSTEM_PRIVILEGE_MAP Description table for privilege type codes. Maps privilege type numbers
to type names
TABLE_PRIVILEGES Grants on objects for which the user is the grantor, grantee, owner, or an
enabled role or PUBLIC is the grantee
TABLE_PRIVILEGE_MAP Description table for privilege (auditing option) type codes. Maps
privilege (auditing option) type numbers to type names
Oracle/PLSQL: Instr Function
Note: If string2 is not found in string1, then the instr Oracle function will return 0.
For example:
instr('Tech on the net', 'e') would return 2; the first occurrence of 'e'
instr('Tech on the net', 'e', 1, 1) would return 2; the first occurrence of 'e'
would return 11; the second occurrence of
instr('Tech on the net', 'e', 1, 2)
'e'
instr('Tech on the net', 'e', 1, 3) would return 14; the third occurrence of 'e'
instr('Tech on the net', 'e', -3, 2) would return 2.
expr1, expr2, . expr_n are expressions that are evaluated by the greatest function.
If the datatypes of the expressions are different, all expressions will be converted to
whatever datatype expr1 is.
If the comparison is based on a character comparison, one character is considered
smaller than another if it has a lower character set value.
For example:
least(2, 5, 12, 3) would return 2
least('2', '5', '12', '3') would return '12'
least('apples', 'oranges', 'bananas') would return 'apples'
least('apples', 'applis', 'applas') would return 'applas'