Академический Документы
Профессиональный Документы
Культура Документы
Learn how to use the Oracle/PLSQL ASCII function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL ASCII function returns the NUMBER code that represents the specified character.
SYNTAX
The syntax for the Oracle/PLSQL ASCII function is:
ASCII( single_character )
PARAMETERS OR ARGUMENTS
single_character is the specified character to retrieve the NUMBER code for. If more than one character
is entered, the ASCII function will return the value for the first character and ignore all of the characters
after the first.
APPLIES TO
The ASCII function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ASCII function examples and explore how to use the ASCII function in
Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ASCIISTR function converts a string in any character set to an ASCII string using the
database character set.
SYNTAX
The syntax for the Oracle/PLSQL ASCIISTR function is:
ASCIISTR( string )
PARAMETERS OR ARGUMENTS
string is a string in any character set that you want converted to an ASCII string in the database character
set.
APPLIES TO
The ASCIISTR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ASCIISTR function examples and explore how to use the ASCIISTR function in
Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL CHR function is the opposite of the ASCII function. It returns the character based on
the NUMBER code.
SYNTAX
The syntax for the Oracle/PLSQL CHR function is:
CHR( number_code )
PARAMETERS OR ARGUMENTS
APPLIES TO
The CHR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle CHR function examples and explore how to use the CHR function in
Oracle/PLSQL.
For example:
CHR(116) woul
d
retur
n 't'
CHR(84) woul
d
retur
n 'T'
COMPOSE FUNCTION
Learn how to use the Oracle/PLSQL COMPOSE function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL COMPOSE function returns a Unicode string.
SYNTAX
The syntax for the Oracle/PLSQL COMPOSE function is:
COMPOSE( string )
PARAMETERS OR ARGUMENTS
string is the input value used to create the Unicode string. It can be a char, varchar2, nchar,
nvarchar2, clob, or nclob.
NOTE
Below is a listing of unistring values that can be combined with other characters in the
compose function.
unistr('\0302') circumflex ( ^ )
unistr('\0303') tilde ( ~ )
unistr('\0308') umlaut ( )
APPLIES TO
The COMPOSE function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
EXAMPLE
Let's look at some Oracle COMPOSE function examples and explore how to use the
COMPOSE function in Oracle/PLSQL.
For example:
CONCAT FUNCTION
Learn how to use the Oracle/PLSQL CONCAT function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL CONCAT function allows you to concatenate two strings together.
SYNTAX
The syntax for the Oracle/PLSQL CONCAT function is:
PARAMETERS OR ARGUMENTS
NOTE
See also the || operator.
APPLIES TO
The CONCAT function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
EXAMPLE
Let's look at some Oracle CONCAT function examples and explore how to use the CONCAT
function in Oracle/PLSQL.
For example:
CONCAT('Tech on', ' the Net'); would return 'Tech on the Net'
Answer: Since the CONCAT function will only let you concatenate 2 strings, you will need to
nest multiple CONCAT functions to concatenate more than 2 strings together.
For example, to concatenate 3 strings, you could nest the CONCAT function as follows:
Or you could nest the CONCAT function as follows, if you wanted to concatenate 4 strings:
Here is an example provided by Ruth that demonstrates how to nest multiple CONCAT
functions to concatenate 6 strings:
'I like ', t.type_desc_column), ' cake with '), t.icing_desc_column),' and
a '),
t.fruit_desc_column)
The CONCAT function is one method to concatenate strings in Oracle. An alternative to using
the CONCAT function would be to use the || operator to catenatenate multiple strings, as
follows:
|| t.fruit_desc_column
|| OPERATOR
Learn how to use the Oracle/PLSQL || concatenate operator with syntax and examples.
DESCRIPTION
The Oracle/PLSQL || operator allows you to concatenate 2 or more strings together.
SYNTAX
The syntax for the Oracle/PLSQL || operator is:
PARAMETERS OR ARGUMENTS
APPLIES TO
The || operator can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle || operator examples and explore how to use the || operator in
Oracle/PLSQL.
For example:
'Tech on' || ' the Net' would return 'Tech on the Net'
DESCRIPTION
The Oracle/PLSQL CONVERT function converts a string from one character set to another.
SYNTAX
The syntax of the CONVERT function is:
PARAMETERS OR ARGUMENTS
NOTE
Character sets include:
APPLIES TO
The CONVERT function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle CONVERT function examples and explore how to use the
CONVERT function in Oracle/PLSQL.
For example:
DECOMPOSE FUNCTION
Learn how to use the Oracle/PLSQL DECOMPOSE function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL DECOMPOSE function accepts a string and returns a Unicode string.
SYNTAX
The syntax for the Oracle/PLSQL DECOMPOSE function is:
DECOMPOSE( string )
PARAMETERS OR ARGUMENTS
APPLIES TO
The DECOMPOSE function can be used in the following versions of Oracle/PLSQL:
For example:
DUMP FUNCTION
Learn how to use the Oracle/PLSQL DUMP function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL DUMP function returns a varchar2 value that includes the datatype code,
the length in bytes, and the internal representation of the expression.
SYNTAX
The syntax for the Oracle/PLSQL DUMP function is:
PARAMETERS OR ARGUMENTS
return_format is optional. It determines the format of the return value. This parameter can be
any of the following values:
Value Explanation
8 octal notation
10 decimal notation
16 hexadecimal notation
17 single characters
start_position and length are optional parameters. They determines which portion of the
internal representation to display. If these parameters are omitted, the DUMP function will
display the entire internal representation in decimal notation.
APPLIES TO
The DUMP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle DUMP function examples and explore how to use the DUMP
function in Oracle/PLSQL.
For example:
INITCAP FUNCTION
Learn how to use the Oracle/PLSQL INITCAP function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL INITCAP function sets the first character in each word to uppercase and
the rest to lowercase.
SYNTAX
The syntax for the Oracle/PLSQL INITCAP function is:
INITCAP( string1 )
PARAMETERS OR ARGUMENTS
string1 is the string argument whose first character in each word will be converted to
uppercase and all remaining characters converted to lowercase.
APPLIES TO
The INITCAP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle INITCAP function examples and explore how to use the INITCAP
function in Oracle/PLSQL.
For example:
INSTR FUNCTION
Learn how to use the Oracle/PLSQL INSTR function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL INSTR function returns the location of a substring in a string.
SYNTAX
The syntax for the Oracle/PLSQL INSTR function is:
PARAMETERS OR ARGUMENTS
string is the string to search. string can be CHAR, VARCHAR2, NCHAR, NVARCHAR2,
CLOB, or NCLOB.
substring is the substring to search for in string. substring can be CHAR, VARCHAR2,
NCHAR, NVARCHAR2, CLOB, or NCLOB.
start_position is the position in string where the search will start. This argument is optional. If
omitted, it defaults to 1. The first position in the string is 1. If thestart_position is negative, the
INSTR function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.
NOTE
If substring is not found in string, then the INSTR function will return 0.
APPLIES TO
The INSTR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle INSTR function examples and explore how to use the INSTR
function in Oracle/PLSQL.
For example:
INSTR2 FUNCTION
Learn how to use the Oracle/PLSQL INSTR2 function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL INSTR2 function returns the location of a substring in a string, using
UCS2 code points.
SYNTAX
The syntax for the Oracle/PLSQL INSTR2 function is:
PARAMETERS OR ARGUMENTS
substring is the substring to search for in string. substring can be CHAR, VARCHAR2,
NCHAR, NVARCHAR2, CLOB, or NCLOB.
start_position is the position in string where the search will start. This argument is optional. If
omitted, it defaults to 1. The first position in the string is 1. If thestart_position is negative, the
INSTR2 function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.
NOTE
If substring is not found in string, then the INSTR2 function will return 0.
APPLIES TO
The INSTR2 function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle INSTR2 function examples and explore how to use the INSTR2
function in Oracle/PLSQL.
For example:
INSTR4 FUNCTION
Learn how to use the Oracle/PLSQL INSTR4 function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL INSTR4 function returns the location of a substring in a string, using
UCS4 code points.
SYNTAX
The syntax for the Oracle/PLSQL INSTR4 function is:
PARAMETERS OR ARGUMENTS
substring is the substring to search for in string. substring can be CHAR, VARCHAR2,
NCHAR, NVARCHAR2, CLOB, or NCLOB.
start_position is the position in string where the search will start. This argument is optional. If
omitted, it defaults to 1. The first position in the string is 1. If thestart_position is negative, the
INSTR4 function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.
NOTE
If substring is not found in string, then the INSTR4 function will return 0.
APPLIES TO
The INSTR4 function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle INSTR4 function examples and explore how to use the INSTR4
function in Oracle/PLSQL.
For example:
INSTRB FUNCTION
Learn how to use the Oracle/PLSQL INSTRB function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL INSTRB function returns the location of a substring in a string, using
bytes instead of characters.
SYNTAX
The syntax for the Oracle/PLSQL INSTRB function is:
PARAMETERS OR ARGUMENTS
string is the string to search. string can be CHAR, VARCHAR2, NCHAR, NVARCHAR2,
CLOB, or NCLOB.
substring is the substring to search for in string. substring can be CHAR, VARCHAR2,
NCHAR, NVARCHAR2, CLOB, or NCLOB.
start_position is the position in string where the search will start. This argument is optional. If
omitted, it defaults to 1. The first position in the string is 1. If thestart_position is negative, the
INSTRB function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.
NOTE
If substring is not found in string, then the INSTRB function will return 0.
APPLIES TO
The INSTRB function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle INSTRB function examples and explore how to use the INSTRB
function in Oracle/PLSQL.
For example:
INSTRC FUNCTION
Learn how to use the Oracle/PLSQL INSTRC function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL INSTRC function returns the location of a substring in a string, using
Unicode complete characters.
SYNTAX
The syntax for the Oracle/PLSQL INSTRC function is:
INSTRC( string, substring [, start_position [, nth_appearance ] ] )
PARAMETERS OR ARGUMENTS
substring is the substring to search for in string. substring can be CHAR, VARCHAR2,
NCHAR, NVARCHAR2, CLOB, or NCLOB.
start_position is the position in string where the search will start. This argument is optional. If
omitted, it defaults to 1. The first position in the string is 1. If thestart_position is negative, the
INSTRC function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.
NOTE
If substring is not found in string, then the INSTRC function will return 0.
APPLIES TO
The INSTRC function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle INSTRC function examples and explore how to use the INSTRC
function in Oracle/PLSQL.
For example:
LENGTH FUNCTION
Learn how to use the Oracle/PLSQL LENGTH function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LENGTH function returns the length of the specified string.
SYNTAX
The syntax for the Oracle/PLSQL LENGTH function is:
LENGTH( string1 )
PARAMETERS OR ARGUMENTS
NOTE
If string1 is NULL, then the LENGTH function will return NULL.
APPLIES TO
The LENGTH function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LENGTH function examples and explore how to use the LENGTH
function in Oracle/PLSQL.
For example:
LENGTH2 FUNCTION
Learn how to use the Oracle/PLSQL LENGTH2 function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LENGTH2 function returns the length of the specified string, using UCS2
code points.
SYNTAX
The syntax for the Oracle/PLSQL LENGTH2 function is:
LENGTH2( string1 )
PARAMETERS OR ARGUMENTS
string1 is the string to return the length for. string1 can be CHAR, VARCHAR2, NCHAR, or
NVARCHAR2. string1 can not be CLOB or NCLOB.
NOTE
If string1 is NULL, then the LENGTH2 function will return NULL.
APPLIES TO
The LENGTH2 function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LENGTH2 function examples and explore how to use the
LENGTH2 function in Oracle/PLSQL.
For example:
LENGTH4 FUNCTION
Learn how to use the Oracle/PLSQL LENGTH4 function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LENGTH4 function returns the length of the specified string, using UCS4
code points.
SYNTAX
The syntax for the Oracle/PLSQL LENGTH4 function is:
LENGTH4( string1 )
PARAMETERS OR ARGUMENTS
string1 is the string to return the length for. string1 can be CHAR, VARCHAR2, NCHAR, or
NVARCHAR2. string1 can not be CLOB or NCLOB.
NOTE
If string1 is NULL, then the LENGTH4 function will return NULL.
APPLIES TO
The LENGTH4 function can be used in the following versions of Oracle/PLSQL:
For example:
LENGTHB FUNCTION
Learn how to use the Oracle/PLSQL LENGTHB function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LENGTHB function returns the length of the specified string, using bytes
instead of characters.
SYNTAX
The syntax for the Oracle/PLSQL LENGTHB function is:
LENGTHB( string1 )
PARAMETERS OR ARGUMENTS
string1 is the string to return the length for. string1 can be CHAR, VARCHAR2, NCHAR,
NVARCHAR2, or single-byte LOB.
NOTE
If string1 is NULL, then the LENGTHB function will return NULL.
APPLIES TO
The LENGTHB function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LENGTHB function examples and explore how to use the
LENGTHB function in Oracle/PLSQL.
For example:
LENGTHC FUNCTION
Learn how to use the Oracle/PLSQL LENGTHC function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LENGTHC function returns the length of the specified string, using
Unicode complete characters.
SYNTAX
The syntax for the Oracle/PLSQL LENGTHC function is:
LENGTHC( string1 )
PARAMETERS OR ARGUMENTS
string1 is the string to return the length for. string1 can be CHAR, VARCHAR2, NCHAR, or
NVARCHAR2. string1 can not be CLOB or NCLOB.
NOTE
If string1 is NULL, then the LENGTHC function will return NULL.
APPLIES TO
The LENGTHC function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LENGTHC function examples and explore how to use the
LENGTHC function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL LOWER function converts all letters in the specified string to lowercase.
If there are characters in the string that are not letters, they are unaffected by this function.
SYNTAX
The syntax for the Oracle/PLSQL LOWER function is:
LOWER( string1 )
PARAMETERS OR ARGUMENTS
NOTE
See also the UPPER function.
APPLIES TO
The LOWER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LOWER function examples and explore how to use the LOWER
function in Oracle/PLSQL.
For example:
LOWER('GEORGE BURNS 123 '); would return 'george burns 123 '
DESCRIPTION
The Oracle/PLSQL LPAD function pads the left-side of a string with a specific set of
characters (when string1 is not null).
SYNTAX
The syntax for the Oracle/PLSQL LPAD function is:
LPAD( string1, padded_length, [ pad_string ] )
PARAMETERS OR ARGUMENTS
padded_length is the number of characters to return. If the padded_length is smaller than the
original string, the LPAD function will truncate the string to the size ofpadded_length.
pad_string is optional. This is the string that will be padded to the left-hand side of string1. If
this parameter is omitted, the LPAD function will pad spaces to the left-side of string1.
APPLIES TO
The LPAD function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LPAD function examples and explore how to use the LPAD function
in Oracle/PLSQL.
For example:
LPAD('tech on the net', 15, 'z'); would return 'tech on the net'
LPAD('tech on the net', 16, 'z'); would return 'ztech on the net'
DESCRIPTION
The Oracle/PLSQL LTRIM function removes all specified characters from the left-hand side
of a string.
SYNTAX
The syntax for the Oracle/PLSQL LTRIM function is:
PARAMETERS OR ARGUMENTS
string1 is the string to trim the characters from the left-hand side.
trim_string is the string that will be removed from the left-hand side of string1. If this
parameter is omitted, the LTRIM function will remove all leading spaces fromstring1.
NOTE
See also the RTRIM and TRIM functions.
APPLIES TO
The LTRIM function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LTRIM function examples and explore how to use the LTRIM
function in Oracle/PLSQL.
For example:
The LTRIM function may appear to remove patterns, but this is not the case as demonstrated
in the following example.
It actually removes the individual occurrences of 'x', 'y', and 'z', as opposed to the pattern of
'xyz'.
The LTRIM function can also be used to remove all leading numbers as demonstrated in the
next example.
DESCRIPTION
The Oracle/PLSQL NCHR function returns the character based on the number_code in the
national character set.
SYNTAX
The syntax for the Oracle/PLSQL NCHR function is:
NCHR( number_code )
PARAMETERS OR ARGUMENTS
number_code is the NUMBER code in the national character set used to retrieve the
character.
NOTE
NCHR( number_code ) is the same as using the CHR function with the USING NCHAR_CS
clause as follows:
APPLIES TO
The NCHR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NCHR function examples and explore how to use the NCHR
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL REPLACE function replaces a sequence of characters in a string with
another set of characters.
SYNTAX
The syntax for the Oracle/PLSQL REPLACE function is:
PARAMETERS OR ARGUMENTS
string1 is the string to replace a sequence of characters with another set of characters.
APPLIES TO
The REPLACE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle REPLACE function examples and explore how to use the
REPLACE function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL RPAD function pads the right-side of a string with a specific set of
characters (when string1 is not null).
SYNTAX
The syntax for the Oracle/PLSQL RPAD function is:
PARAMETERS OR ARGUMENTS
string1 is the string to pad characters to (the right-hand side).
padded_length is the number of characters to return. If the padded_length is smaller than the
original string, the RPAD function will truncate the string to the size ofpadded_length.
pad_string is optional. This is the string that will be padded to the right-hand side of string1. If
this parameter is omitted, the RPAD function will pad spaces to the right-side of string1.
APPLIES TO
The RPAD function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle RPAD function examples and explore how to use the RPAD
function in Oracle/PLSQL.
For example:
RPAD('tech on the net', 15, 'z'); would return 'tech on the net'
RPAD('tech on the net', 16, 'z'); would return 'tech on the netz'
DESCRIPTION
The Oracle/PLSQL RTRIM function removes all specified characters from the right-hand
side of a string.
SYNTAX
The syntax for the Oracle/PLSQL RTRIM function is:
RTRIM( string1, [ trim_string ] )
PARAMETERS OR ARGUMENTS
string1 is the string to trim the characters from the right-hand side.
trim_string is the string that will be removed from the right-hand side of string1. If this
parameter is omitted, the RTRIM function will remove all trailing spaces fromstring1.
NOTE
See also the LTRIM and TRIM functions.
APPLIES TO
The RTRIM function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle RTRIM function examples and explore how to use the RTRIM
function in Oracle/PLSQL.
For example:
The RTRIM function may appear to remove patterns, but this is not the case as
demonstrated in the following example.
RTRIM('Techxyxxyzyyyxx', 'xyz'); would return 'Tech'
It actually removes the individual occurrences of 'x', 'y', and 'z', as opposed to the pattern of
'xyz'.
The RTRIM function can also be used to remove all trailing numbers as demonstrated in the
next example.
DESCRIPTION
The Oracle/PLSQL SOUNDEX function returns a phonetic representation (the way it
sounds) of a string.
SYNTAX
The syntax for the Oracle/PLSQL SOUNDEX function is:
SOUNDEX( string1 )
PARAMETERS OR ARGUMENTS
NOTE
The SOUNDEX function algorithm is as follows:
1. The SOUNDEX function return value will always begin with the first letter of string1.
2. The SOUNDEX function uses only the first 5 consonants to determine the NUMERIC
portion of the return value, except if the first letter of string1 is a vowel.
3. The SOUNDEX function is not case-sensitive. What this means is that both uppercase
and lowercase characters will generate the same SOUNDEX function return value.
APPLIES TO
The SOUNDEX function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SOUNDEX function examples and explore how to use the
SOUNDEX function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL SUBSTR functions allows you to extract a substring from a string.
SYNTAX
The syntax for the Oracle/PLSQL SUBSTR function is:
PARAMETERS OR ARGUMENTS
start_position is the position for extraction. The first position in the string is always 1.
length is optional. It is the number of characters to extract. If this parameter is omitted, the
SUBSTR function will return the entire string.
NOTE
If start_position is 0, then the SUBSTR function treats start_position as 1 (ie: the first
position in the string).
If start_position is a positive number, then the SUBSTR function starts from the
beginning of the string.
If start_position is a negative number, then the SUBSTR function starts from the end of
the string and counts backwards.
If length is a negative number, then the SUBSTR function will return a NULL value.
APPLIES TO
The SUBSTR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SUBSTR function examples and explore how to use the SUBSTR
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_CHAR function converts a number or date to a string.
SYNTAX
The syntax for the Oracle/PLSQL TO_CHAR function is:
PARAMETERS OR ARGUMENTS
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.
APPLIES TO
The TO_CHAR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_CHAR function examples and explore how to use the
TO_CHAR function in Oracle/PLSQL.
WITH NUMBERS
For example:
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
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.
MI Minute (0-59).
SS Second (0-59).
FF Fractional seconds.
You will notice that in some TO_CHAR function examples, the format_mask parameter
begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the
examples below.
The zeros have been suppressed so that the day component shows as "9" as opposed to
"09".
FROM emp
ORDER BY "Day";
Answer: In the above SQL, the fmDay format mask used in the TO_CHAR function will
return the name of the Day and not the numeric value of the day.
To sort the days of the week in order, you need to return the numeric value of the day by
using the fmD format mask as follows:
FROM emp
ORDER BY "Day";
DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.
SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a date.
nls_language is optional. This is the nls language used to convert string1 to a date.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
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.
MI Minute (0-59).
SS Second (0-59).
AD or A.D AD indicator
BC or B.C. BC indicator
APPLIES TO
The TO_DATE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.
SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:
format_mask is optional. This is the format that will be used to convert string1 to a date.
nls_language is optional. This is the nls language used to convert string1 to a date.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
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.
MI Minute (0-59).
SS Second (0-59).
AD or A.D AD indicator
BC or B.C. BC indicator
APPLIES TO
The TO_DATE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TRIM function removes all specified characters either from the beginning
or the ending of a string.
SYNTAX
The syntax for the Oracle/PLSQL TRIM function is:
PARAMETERS OR ARGUMENTS
trim_character is the character that will be removed from string1. If this parameter is omitted,
the TRIM function will remove space characters from string1.
NOTE
If you do not choose a value for the first parameter (LEADING, TRAILING, BOTH), the
TRIM function will remove trim_character from both the front and end of string1.
See also the LTRIM and RTRIM functions.
APPLIES TO
The TRIM function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TRIM function examples and explore how to use the TRIM function
in Oracle/PLSQL.
For example:
TRIM(' tech ') would return 'tech'
DESCRIPTION
The Oracle/PLSQL UPPER function converts all letters in the specified string to uppercase.
If there are characters in the string that are not letters, they are unaffected by this function.
SYNTAX
The syntax for the Oracle/PLSQL UPPER function is:
UPPER( string1 )
PARAMETERS OR ARGUMENTS
NOTE
See also the LOWER function.
APPLIES TO
The UPPER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle UPPER function examples and explore how to use the UPPER
function in Oracle/PLSQL.
For example:
UPPER('george burns 123 '); would return 'GEORGE BURNS 123 '
Let's say that we have a suppliers table with a field called supplier_name that contains the
values TEST, Test, or test.
If we wanted to find all records containing the word "test", regardless of whether it was stored
as TEST, Test, or test, we could run either of the following SQL statements:
OR
These SQL statements use a combination of the UPPER function and the LIKE condition to
return all of the records where the supplier_name field contains the word "test", regardless of
whether it was stored as TEST, Test, or test.
DESCRIPTION
The Oracle/PLSQL VSIZE function returns the number of bytes in the internal representation
of an expression.
SYNTAX
The syntax for the Oracle/PLSQL VSIZE function is:
VSIZE( expression )
PARAMETERS OR ARGUMENTS
APPLIES TO
The VSIZE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle VSIZE function examples and explore how to use the VSIZE
function in Oracle/PLSQL.
For example:
CONVERSION FUNCTIONS
DESCRIPTION
The Oracle/PLSQL BIN_TO_NUM function converts a bit vector to a number.
SYNTAX
The syntax for the Oracle/PLSQL BIN_TO_NUM function is:
PARAMETERS OR ARGUMENTS
expr1, expr2, ... expr_n must be either 0 or 1 values. They represent bits in a bit vector.
APPLIES TO
The BIN_TO_NUM function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle BIN_TO_NUM function examples and explore how to use the
BIN_TO_NUM function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL CAST function converts one datatype to another.
SYNTAX
The syntax for the Oracle/PLSQL CAST function is:
NOTE
The following casts are allowed:
TO FROM
char, X X X X X
varchar2
number X X
datetime / X X
interval
raw X X
rowid, urowid X X
nchar, X X X X X
nvarchar2
APPLIES TO
The CAST function can be used in the following versions of Oracle/PLSQL:
For example:
from dual;
This would convert the date (ie: 22-Aug-2003) into a varchar2(30) value.
DESCRIPTION
The Oracle/PLSQL CHARTOROWID function converts a char, varchar2, nchar, or nvarchar2
to a rowid.
SYNTAX
The syntax for the Oracle/PLSQL CHARTOROWID function is:
CHARTOROWID( value1 )
PARAMETERS OR ARGUMENTS
BBBBBBB.RRRR.FFFFF
where:
APPLIES TO
The CHARTOROWID function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
EXAMPLE
Let's look at some Oracle CHARTOROWID function examples and explore how to use the
CHARTOROWID function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL FROM_TZ function converts a TIMESTAMP value (given a TIME ZONE)
to a TIMESTAMP WITH TIME ZONE value.
SYNTAX
The syntax for the Oracle/PLSQL FROM_TZ function is:
PARAMETERS OR ARGUMENTS
APPLIES TO
The FROM_TZ function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle FROM_TZ function examples and explore how to use the
FROM_TZ function in Oracle/PLSQL.
For example:
from dual;
If you executed the same SQL statement, but set the time zone to '-7:00' as follows:
from dual;
DESCRIPTION
The Oracle/PLSQL HEXTORAW function converts a hexadecimal value into a raw value.
SYNTAX
The syntax for the Oracle/PLSQL HEXTORAW function is:
HEXTORAW( char )
PARAMETERS OR ARGUMENTS
APPLIES TO
The HEXTORAW function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle HEXTORAW function examples and explore how to use the
HEXTORAW function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: NUMTODSINTERVAL
FUNCTION
Learn how to use the Oracle/PLSQL NUMTODSINTERVAL function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL NUMTODSINTERVAL function converts a number to an INTERVAL
DAY TO SECOND literal.
SYNTAX
The syntax for the Oracle/PLSQL NUMTODSINTERVAL function is:
PARAMETERS OR ARGUMENTS
expression is the unit. It must be one of the following values: DAY, HOUR, MINUTE, or
SECOND.
APPLIES TO
The NUMTODSINTERVAL function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
EXAMPLE
Let's look at some Oracle NUMTODSINTERVAL function examples and explore how to use
the NUMTODSINTERVAL function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: NUMTOYMINTERVAL
FUNCTION
Learn how to use the Oracle/PLSQL NUMTOYMINTERVAL function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL NUMTOYMINTERVAL function converts a number to an INTERVAL
YEAR TO MONTH literal.
SYNTAX
The syntax for the Oracle/PLSQL NUMTOYMINTERVAL function is:
PARAMETERS OR ARGUMENTS
expression is the unit. It must be one of the following values: YEAR or MONTH.
APPLIES TO
The NUMTOYMINTERVAL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NUMTOYMINTERVAL function examples and explore how to use
the NUMTOYMINTERVAL function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL RAWTOHEX function converts a raw value into a hexadecimal value.
One of our viewers says that this function comes in handy to move a varchar value to a blob
field.
SYNTAX
The syntax for the Oracle/PLSQL RAWTOHEX function is:
RAWTOHEX( raw )
PARAMETERS OR ARGUMENTS
NOTE
This function works differently when used as a PLSQL built-in function as opposed to running
it in SQL. As a PLSQL function, the RAWTOHEX function may perform an implicit conversion
before converting to a hexadecimal value. This may result in a different value being returned
by this function between PLSQL and SQL.
a varchar2(8);
BEGIN
a := rawtohex('AB');
dbms_output.put_line(a);
dbms_output.put_line(a);
END;
AB
4142
The reason for the difference is that PLSQL is doing an implicit conversion of 'AB' into a RAW
(treats 'AB' as a single byte equal to CHR(171)). A RAWTOHEX on that returns the string
'AB'.
Whereas, SQL is not doing that implicit conversion. 'AB' is 2 byte RAW already and a
RAWTOHEX of that retuns 4142.
APPLIES TO
The RAWTOHEX function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle RAWTOHEX function examples and explore how to use the
RAWTOHEX function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_CHAR function converts a number or date to a string.
SYNTAX
The syntax for the Oracle/PLSQL TO_CHAR function is:
PARAMETERS OR ARGUMENTS
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.
APPLIES TO
The TO_CHAR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_CHAR function examples and explore how to use the
TO_CHAR function in Oracle/PLSQL.
WITH NUMBERS
For example:
WITH DATES
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
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.
MI Minute (0-59).
SS Second (0-59).
FF Fractional seconds.
You will notice that in some TO_CHAR function examples, the format_mask parameter
begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the
examples below.
FROM emp
ORDER BY "Day";
Answer: In the above SQL, the fmDay format mask used in the TO_CHAR function will
return the name of the Day and not the numeric value of the day.
To sort the days of the week in order, you need to return the numeric value of the day by
using the fmD format mask as follows:
FROM emp
ORDER BY "Day";
DESCRIPTION
The Oracle/PLSQL TO_CLOB function converts a LOB value from the national character set
to the database character set.
SYNTAX
The syntax for the Oracle/PLSQL TO_CLOB function is:
TO_CLOB( expression )
PARAMETERS OR ARGUMENTS
expression can either be a lob_column, char, varchar2, nchar, nvarchar2, clob or nclob value.
APPLIES TO
The TO_CLOB function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_CLOB function examples and explore how to use the
TO_CLOB function in Oracle/PLSQL.
For example:
select TO_CLOB(nclob_column)
from suppliers;
This example would convert the value in the field called nclob_column to a CLOB value.
DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.
SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a date.
nls_language is optional. This is the nls language used to convert string1 to a date.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
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.
MI Minute (0-59).
SS Second (0-59).
AD or A.D AD indicator
BC or B.C. BC indicator
APPLIES TO
The TO_DATE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_DSINTERVAL function converts a string to an INTERVAL DAY TO
SECOND type.
SYNTAX
The syntax for the Oracle/PLSQL TO_DSINTERVAL function is:
PARAMETERS OR ARGUMENTS
character is the value to convert to an INTERVAL DAY TO SECOND type. It can be a char,
varchar2, nchar, or nvarchar2 value.
APPLIES TO
The TO_DSINTERVAL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_DSINTERVAL function examples and explore how to use the
TO_DSINTERVAL function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_LOB function converts LONG or LONG RAW values to LOB values.
SYNTAX
The syntax for the Oracle/PLSQL TO_LOB function is:
TO_LOB( long_column )
PARAMETERS OR ARGUMENTS
APPLIES TO
The TO_LOB function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_LOB function examples and explore how to use the TO_LOB
function in Oracle/PLSQL.
For example:
select TO_LOB(long_column)
from suppliers;
This example would convert the value in the field called long_column to a LOB value.
DESCRIPTION
The Oracle/PLSQL TO_MULTI_BYTE function returns a character value with all of the
single-byte characters converted to multibyte characters. To use this function, your database
character set contains both single-byte and multibyte characters.
SYNTAX
The syntax for the Oracle/PLSQL TO_MULTI_BYTE function is:
TO_MULTI_BYTE( char )
PARAMETERS OR ARGUMENTS
char can be a char, varchar2, nchar, or nvarchar2 value. This function will return its result in
the same character set as char.
APPLIES TO
The TO_MULTI_BYTE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_MULTI_BYTE function examples and explore how to use the
TO_MULTI_BYTE function in Oracle/PLSQL.
For example:
from dual;
DESCRIPTION
The Oracle/PLSQL TO_NCLOB function converts a LOB value to a NCLOB value.
SYNTAX
The syntax for the Oracle/PLSQL TO_NCLOB function is:
TO_NCLOB( expression )
PARAMETERS OR ARGUMENTS
APPLIES TO
The TO_NCLOB function can be used in the following versions of Oracle/PLSQL:
For example:
select TO_NCLOB(lob_column)
from suppliers;
This example would convert the value in the field called lob_column to an NCLOB value.
DESCRIPTION
The Oracle/PLSQL TO_NUMBER function converts a string to a number.
SYNTAX
The syntax for the Oracle/PLSQL TO_NUMBER function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a number.
nls_language is optional. This is the nls language used to convert string1 to a number.
APPLIES TO
The TO_NUMBER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_NUMBER function examples and explore how to use the
TO_NUMBER function in Oracle/PLSQL.
For example:
Since the format_mask and nls_language parameters are optional, you can simply convert a
text string to a numeric value as follows:
DESCRIPTION
The Oracle/PLSQL TO_SINGLE_BYTE function returns a character value with all of the
multibyte characters converted to single-byte characters. To use this function, your database
character set contains both single-byte and multibyte characters.
SYNTAX
The syntax for the Oracle/PLSQL TO_SINGLE_BYTE function is:
TO_SINGLE_BYTE( char )
PARAMETERS OR ARGUMENTS
char can be a char, varchar2, nchar, or nvarchar2 value. This function will return its result in
the same character set as char.
APPLIES TO
The TO_SINGLE_BYTE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_SINGLE_BYTE function examples and explore how to use the
TO_SINGLE_BYTE function in Oracle/PLSQL.
For example:
from dual;
DESCRIPTION
The Oracle/PLSQL TO_TIMESTAMP function converts a string to a timestamp.
SYNTAX
The syntax for the Oracle/PLSQL TO_TIMESTAMP function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a timestamp.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
MI Minute (0-59).
SS Second (0-59).
APPLIES TO
The TO_TIMESTAMP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_TIMESTAMP function examples and explore how to use the
TO_TIMESTAMP function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: TO_TIMESTAMP_TZ
FUNCTION
Learn how to use the Oracle/PLSQL TO_TIMESTAMP_TZ function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL TO_TIMESTAMP_TZ function converts a string to a timestamp with time
zone.
SYNTAX
The syntax for the Oracle/PLSQL TO_TIMESTAMP_TZ function is:
PARAMETERS OR ARGUMENTS
string1 is the string that will be converted to a timestamp with time zone.
format_mask is optional. This is the format that will be used to convert string1 to a timestamp
with time zone.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
SS Second (0-59).
APPLIES TO
The TO_TIMESTAMP_TZ function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_TIMESTAMP_TZ function examples and explore how to use
the TO_TIMESTAMP_TZ function in Oracle/PLSQL.
For example:
would also return '13-DEC-03 10.13.18.000000000 AM -08:00' as a timestamp with time zone
value.
DESCRIPTION
The Oracle/PLSQL TO_YMINTERVAL function converts a string to an INTERVAL YEAR TO
MONTH type.
SYNTAX
The syntax for the Oracle/PLSQL TO_YMINTERVAL function is:
TO_YMINTERVAL( character )
PARAMETERS OR ARGUMENTS
character is the value to convert to an INTERVAL YEAR TO MONTH type. It can be a char,
varchar2, nchar, or nvarchar2 value.
APPLIES TO
The TO_YMINTERVAL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_YMINTERVAL function examples and explore how to use the
TO_YMINTERVAL function in Oracle/PLSQL.
For example:
The TO_YMINTERVAL function is most commonly used to add an interval to a date field. For
example, you may wish to add 1 year and 5 months to an order date.
from orders;
This SQL statement would return the order date, as well as the order date plus 1 year and 5
months.
ADVANCED FUNCTIONS
ORACLE/PLSQL: BFILENAME FUNCTION
Learn how to use the Oracle/PLSQL BFILENAME function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL BFILENAME function returns a BFILE locator for a physical LOB binary
file.
SYNTAX
The syntax for the Oracle/PLSQL BFILENAME function is:
PARAMETERS OR ARGUMENTS
directory is a directory object that serves as an alias for the full path to where the file is
located on the file server.
APPLIES TO
The BFILENAME function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle BFILENAME function examples and explore how to use the
BFILENAME function in Oracle/PLSQL.
For example:
First, we need to create a directory object called exampleDir that points to /example/totn on
the file server.
Then we can use the exampleDir directory object in the BFILENAME function as follows:
DESCRIPTION
The Oracle/PLSQL CARDINALITY function returns the number of elements in a nested
table.
SYNTAX
The syntax for the Oracle/PLSQL CARDINALITY function is:
PARAMETERS OR ARGUMENTS
nested table column is the column in the nested table that you wish to return the cardinality
for.
NOTE
If the nested table is empty, the CARDINALITY function will return NULL.
If the nested table is a null collection, the CARDINALITY function will return NULL.
APPLIES TO
The CARDINALITY function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle CARDINALITY function examples and explore how to use the
CARDINALITY function in Oracle/PLSQL.
For example:
from suppliers;
ORACLE/PLSQL: CASE STATEMENT
Learn how to use the Oracle/PLSQL CASE statement with syntax and examples.
DESCRIPTION
The Oracle/PLSQL CASE statement has the functionality of an IF-THEN-ELSE statement.
Starting in Oracle 9i, you can use the CASE statement within a SQL statement.
SYNTAX
The syntax for the Oracle/PLSQL CASE statement is:
CASE [ expression ]
...
ELSE result
END
PARAMETERS OR ARGUMENTS
expression is optional. It is the value that you are comparing to the list of conditions. (ie:
condition_1, condition_2, ... condition_n)
condition_1 to condition_n must all be the same datatype. Conditions are evaluated in the
order listed. Once a condition is found to be true, the CASE statement will return the result
and not evaluate the conditions any further.
result_1 to result_n must all be the same datatype. This is the value returned once
a condition is found to be true.
NOTE
If no condition is found to be true, then the CASE statement will return the value in the ELSE
clause.
If the ELSE clause is omitted and no condition is found to be true, then the CASE statement
will return NULL.
You can have up to 255 comparisons in a CASE statement. Each WHEN ... THEN clause is
considered 2 comparisons.
APPLIES TO
The CASE statement can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The CASE statement can be used in Oracle/PLSQL.
You could use the CASE statement in a SQL statement as follows: (includes
the expression clause)
SELECT table_name,
CASE owner
END
FROM all_tables;
Or you could write the SQL statement using the CASE statement like this: (omits
the expression clause)
SELECT table_name,
CASE
END
FROM all_tables;
The above two CASE statements are equivalent to the following IF-THEN-ELSE statement:
ELSE
END IF;
The CASE statement will compare each owner value, one by one.
One thing to note is that the ELSE clause within the CASE statement is optional. You could
have omitted it. Let's look at the SQL statement above with the ELSE clause omitted.
SELECT table_name,
CASE owner
END
FROM all_tables;
With the ELSE clause omitted, if no condition was found to be true, the CASE statement
would return NULL.
COMPARING 2 CONDITIONS
Here is an example that demonstrates how to use the CASE statement to compare different
conditions:
SELECT
CASE
END
FROM suppliers;
Answer: Yes, below is an example of a case statement that evaluates two different fields.
SELECT supplier_id,
CASE
END
FROM suppliers;
So if supplier_name field is IBM and the supplier_type field is Hardware, then the CASE
statement will return North office. If the supplier_name field is IBM and the supplier_type
is Software, the CASE statement will return South office.
DESCRIPTION
The Oracle/PLSQL COALESCE function returns the first non-null expression in the list. If all
expressions evaluate to null, then the COALESCE function will return null.
SYNTAX
The syntax for the Oracle/PLSQL COALESCE function is:
PARAMETERS OR ARGUMENTS
APPLIES TO
The COALESCE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The COALESCE function can be used in Oracle/PLSQL.
FROM suppliers;
result := address1;
result := address2;
ELSIF address3 is not null THEN
result := address3;
ELSE
result := null;
END IF;
DESCRIPTION
The Oracle/PLSQL DECODE function has the functionality of an IF-THEN-ELSE statement.
SYNTAX
The syntax for the Oracle/PLSQL DECODE function is:
PARAMETERS OR ARGUMENTS
default is optional. If no matches are found, the DECODE function will return default.
If default is omitted, then the DECODE function will return null (if no matches are found).
APPLIES TO
The DECODE function can be used in the following versions of Oracle/PLSQL:
SELECT supplier_name,
10001, 'Microsoft',
'Gateway') result
FROM suppliers;
result := 'IBM';
result := 'Microsoft';
ELSE
result := 'Gateway';
END IF;
The DECODE function will compare each supplier_id value, one by one.
Helpful Tip #1: One of our viewers suggested combining the SIGN function with the
DECODE function as follows:
The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses
Helpful Tip #2: One of our viewers suggested using the LEAST function (instead of the
DECODE function) as follows:
LEAST(date1, date2)
Question: I would like to know if it's possible to use the DECODE function 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 function 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,
1, 'category 2',
2, 'category 3',
'unknown') result
FROM suppliers;
TRUNC ((supplier_id - 1) / 10
and so on...
Question: I need to write a DECODE statement that will return the following:
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,
1, 0.04,
0.06) as perc_value
FROM employees;
Question: Is there a limit to the number of arguments that you can have in one DECODE
statement? I'm getting an error, "ORA-00939: too many arguments for function".
Answer: Yes, the maximum number of components that you can have in a DECODE
function is 255. This includes the expression, search, and result arguments.
DESCRIPTION
The Oracle/PLSQL EMPTY_BLOB function can be used to initialize a LOB column to
EMPTY in an INSERT statement or UPDATE statement or it can be used initalize a LOB
variable.
SYNTAX
The syntax for the Oracle/PLSQL EMPTY_BLOB function is:
EMPTY_BLOB ()
PARAMETERS OR ARGUMENTS
APPLIES TO
The EMPTY_BLOB function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle EMPTY_BLOB function examples and explore how to use the
EMPTY_BLOB function in Oracle/PLSQL.
For example:
UPDATE products
DESCRIPTION
The Oracle/PLSQL EMPTY_CLOB function can be used to initialize a LOB column to
EMPTY in an INSERT statement or UPDATE statement or it can be used initalize a LOB
variable.
SYNTAX
The syntax for the Oracle/PLSQL EMPTY_CLOB function is:
EMPTY_CLOB ()
PARAMETERS OR ARGUMENTS
APPLIES TO
The EMPTY_CLOB function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle EMPTY_CLOB function examples and explore how to use the
EMPTY_CLOB function in Oracle/PLSQL.
For example:
UPDATE customers
This EMPTY_CLOB example would initialize the customer_photo column to EMPTY. This is
different than setting the column to a null LOB or a LOB with a string length of 0.
For each unique group, the GROUP_ID function will return 0. When a duplicated group is
found, the GROUP_ID function will return a value >0.
SYNTAX
The syntax for the Oracle/PLSQL GROUP_ID function is:
FROM tables
WHERE conditions
PARAMETERS OR ARGUMENTS
APPLIES TO
The GROUP_ID function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle GROUP_ID function examples and explore how to use the
GROUP_ID function in Oracle/PLSQL.
For example:
FROM employees
GROUP BY department,
ROLLUP (department, bonus);
You could use the HAVING clause to eliminated duplicated groups as follows:
FROM employees
GROUP BY department,
DESCRIPTION
The Oracle/PLSQL LAG function is an analytic function that lets you query more than one
row in a table at a time without having to join the table to itself. It returns values from a
previous row in the table. To return a value from the next row, try using the LEAD function.
SYNTAX
The syntax for the Oracle/PLSQL LAG function is:
PARAMETERS OR ARGUMENTS
expression is an expression that can contain other built-in functions, but can not contain any
analytic functions.
offset is optional. It is the physical offset from the current row in the table. If this parameter is
omitted, the default is 1.
default is optional. It is the value that is returned if the offset goes out of the bounds of the
table. If this parameter is omitted, the default is null.
APPLIES TO
The LAG function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The LAG function can be used in Oracle/PLSQL.
Let's look at an example. If we had an orders table that contained the following data:
25/09/2007 1000 20
26/09/2007 2000 15
27/09/2007 1000 8
28/09/2007 2000 12
29/09/2007 2000 2
30/09/2007 1000 4
from orders;
If we had used an offset of 2 instead, it would have returned the order_date from 2 orders
before. If we had used an offset of 3, it would have returned the order_date from 3 orders
before....and so on.
If we wanted only the orders for a given product_id, we could run the following SQL
statement:
from orders
In this example, it returned the previous order_date for product_id = 2000 and ignored all
other orders.
ORACLE/PLSQL: LEAD FUNCTION
Learn how to use the Oracle/PLSQL LEAD function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LEAD function is an analytic function that lets you query more than one
row in a table at a time without having to join the table to itself. It returns values from the next
row in the table. To return a value from a previous row, try using the LAG function.
SYNTAX
The syntax for the Oracle/PLSQL LEAD function is:
PARAMETERS OR ARGUMENTS
expression is an expression that can contain other built-in functions, but can not contain any
analytic functions.
offset is optional. It is the physical offset from the current row in the table. If this parameter is
omitted, the default is 1.
default is optional. It is the value that is returned if the offset goes out of the bounds of the
table. If this parameter is omitted, the default is null.
APPLIES TO
The LEAD function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The LEAD function can be used in Oracle/PLSQL.
Let's look at an example. If we had an orders table that contained the following data:
25/09/2007 1000 20
26/09/2007 2000 15
27/09/2007 1000 8
28/09/2007 2000 12
29/09/2007 2000 2
30/09/2007 1000 4
from orders;
If we wanted only the orders for a given product_id, we could run the following SQL
statement:
from orders
In this example, it returned the next order_date for product_id = 2000 and ignored all other
orders.
DESCRIPTION
The Oracle/PLSQL LISTAGG function concatenates values of the measure_column for each
GROUP based on the order_by_clause.
SYNTAX
The syntax for the Oracle/PLSQL LISTAGG function is:
PARAMETERS OR ARGUMENTS
measure_column is the column whose values you wish to concatenate together in the result
set. Null values in the measure_column are ignored.
delimiter is optional. It is the delimiter to use when separating the measure_column values
when outputting the results.
order_by_clause determines the order that the concatenated values (ie: measure_column)
are returned.
APPLIES TO
The LISTAGG function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The LISTAGG function can be used in Oracle/PLSQL.
Since this is a more complicated function to understand, let's look at an example that includes
data to demonstrate what the function outputs.
product_id product_name
1001 Bananas
1002 Apples
1003 Pears
1004 Oranges
And then you executed a SELECT statement using the LISTAGG function:
Product_Listing
In this example, the results of the LISTAGG function are output in a single field with the
values comma delimited.
You can change the ORDER BY clause to use the DESC keyword and change the SELECT
statement as follows:
FROM products;
Product_Listing
FROM products;
Product_Listing
DESCRIPTION
The Oracle/PLSQL LNNVL function is used in the WHERE clause of a SQL statement to
evaluate a condition when one of the operands may contain a NULL value.
SYNTAX
The syntax for the Oracle/PLSQL LNNVL function is:
LNNVL( condition )
NOTE
The LNNVL function will return the following:
TRUE FALSE
FALSE TRUE
UNKNOWN TRUE
So if we had two columns called qty and reorder_level where qty = 20 and reorder_level IS
NULL, the LNNVL function would return the following:
APPLIES TO
The LNNVL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The LNNVL function can be used in Oracle/PLSQL.
Let's look at an example. If we had an products table that contained the following data:
1000 20 NULL
2000 15 8
3000 8 10
4000 12 6
5000 2 2
6000 4 5
And we wanted to find all of the products whose qty was below their respective reorder levels,
we would run the following SQL statement:
3000 8 10
6000 4 5
However, if we wanted to see the products that were below their reorder levels as well as
NULL reorder levels, we would use the LNNVL function as follows:
1000 20 NULL
3000 8 10
6000 4 5
In this example, the result set also contains the product_id of 1000 which has a NULL reorder
level.
DESCRIPTION
The Oracle/PLSQL NANVL function lets you substitute a value for a floating point number
such as BINARY_FLOAT or BINARY_DOUBLE, when a Nan (Not a number) value is
encountered. This is most commonly used to convert Nan (Not a number) values into either
NULL or 0.
SYNTAX
The syntax for the Oracle/PLSQL NANVL function is:
NANVL( value, replace_with )
PARAMETERS OR ARGUMENTS
APPLIES TO
The NANVL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
The NANVL function can be used in Oracle/PLSQL.
For example:
select NANVL(binary1, 0)
from test_table;
The SQL statement above would return 0 if the binary1 field contained a Nan (Not a number)
value. Otherwise, it would return the binary1 value.
from test_table;
The SQL statement above would return NULL if the binary1 field contained a Nan (Not a
number) value. Otherwise, it would return the binary1 value.
DESCRIPTION
The Oracle/PLSQL NULLIF function compares expr1 and expr2. If expr1 and expr2 are
equal, the NULLIF function returns NULL. Otherwise, it returns expr1.
SYNTAX
The syntax for the Oracle/PLSQL NULLIF function is:
PARAMETERS OR ARGUMENTS
expr1 and expr2 must be either numeric values or values that are of the same datatype.
NOTE
expr1 can be an expression that evaluates to NULL, but it can not be the literal NULL.
APPLIES TO
The NULLIF function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NULLIF function examples and explore how to use the NULLIF
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL NVL function lets you substitute a value when a null value is
encountered.
SYNTAX
The syntax for the Oracle/PLSQL NVL function is:
PARAMETERS OR ARGUMENTS
APPLIES TO
The NVL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NVL function examples and explore how to use the NVL function in
Oracle/PLSQL.
For example:
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.
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.
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.
To be precise,
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:
FROM employees;
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:
Answer: You will need to make separate NVL function calls for each column. For example:
DESCRIPTION
The Oracle/PLSQL NVL2 function extends the functionality found in the NVL function. It lets
you substitutes a value when a null value is encountered as well as when a non-null value is
encountered.
SYNTAX
The syntax for the Oracle/PLSQL NVL2 function is:
PARAMETERS OR ARGUMENTS
APPLIES TO
The NVL2 function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NVL2 function examples and explore how to use the NVL2 function
in Oracle/PLSQL.
For example:
from suppliers;
The SQL statement above would return 'n/a' if the supplier_city field contained a null value.
Otherwise, it would return the 'Completed'.
Another example using the NVL2 function in Oracle/PLSQL is:
select supplier_id,
from suppliers;
This SQL statement would return the supplier_name2 field if the supplier_desc contained a
null value. Otherwise, it would return the supplier_name field.
DESCRIPTION
The Oracle/PLSQL SYS_CONTEXT function can be used to retrieve information about the
Oracle environment.
SYNTAX
The syntax for the Oracle/PLSQL SYS_CONTEXT function is:
PARAMETERS OR ARGUMENTS
namespace is an Oracle namespace that has already been created. If the namespace of
'USERENV' is used, attributes describing the current Oracle session can be returned.
parameter is a valid attribute that has been set using the DBMS_SESSION.set_context
procedure.
length is optional. It is the length of the return value in bytes. If this parameter is omitted or if
an invalid entry is provided, the sys_context function will default to 256 bytes.
NOTE
The valid parameters for the namespace called 'USERENV' are as follows: (Note that not all
parameters are valid in all versions of Oracle)
APPLIES TO
The SYS_CONTEXT function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SYS_CONTEXT function examples and explore how to use the
SYS_CONTEXT function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL UID function returns the id number for a user's session (the user who is
currently logged in).
SYNTAX
The syntax for the Oracle/PLSQL UID function is:
UID
PARAMETERS OR ARGUMENTS
APPLIES TO
The UID function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle UID function examples and explore how to use the UID function in
Oracle/PLSQL.
For example:
SELECT UID
INTO parm_uid
FROM dual;
The variable called parm_uid will now contain the id number for the user's session. The value
could be 25, for example.
DESCRIPTION
The Oracle/PLSQL USER function returns the user_id from the current Oracle session.
SYNTAX
The syntax for the Oracle/PLSQL USER function is:
USER
PARAMETERS OR ARGUMENTS
EXAMPLE
Let's look at some Oracle USER function examples and explore how to use the USER
function in Oracle/PLSQL.
For example:
parm_user_ID := USER;
OR
select USER
into parm_user_ID
from dual;
The variable called parm_user_ID will now contain the Oracle user ID from the current Oracle
session.
DESCRIPTION
The Oracle/PLSQL USERENV function can be used to retrieve information about the current
Oracle session. Although this function still exists in Oracle for backwards compatibility, it is
recommended that you use the sys_context function instead.
SYNTAX
The syntax for the Oracle/PLSQL USERENV function is:
USERENV( parameter )
PARAMETERS OR ARGUMENTS
parameter is the value to return from the current Oracle session. The possible values are:
Parameter Explanation
ISDBA Returns TRUE if the user has DBA privileges. Otherwise, it will return FALSE.
LANGUAGE The language, territory, and character of the session. In the following
format:
language_territory.characterset
APPLIES TO
The USERENV function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle USERENV function examples and explore how to use the
USERENV function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ABS function returns the absolute value of a number.
SYNTAX
The syntax for the Oracle/PLSQL ABS function is:
ABS( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The ABS function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ABS function examples and explore how to use the ABS function in
Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ACOS function returns the arc cosine of a number.
SYNTAX
The syntax for the Oracle/PLSQL ACOS function is:
ACOS( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The ACOS function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ACOS function examples and explore how to use the ACOS
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ASIN function returns the arc sine of a number.
SYNTAX
The syntax for the Oracle/PLSQL ASIN function is:
ASIN( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The ASIN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ASIN function examples and explore how to use the ASIN function
in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ATAN function returns the arc tangent of a number.
SYNTAX
The syntax for the Oracle/PLSQL ATAN function is:
ATAN( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The ATAN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ATAN function examples and explore how to use the ATAN
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ATAN2 function returns the arc tangent of n and m.
SYNTAX
The syntax for the Oracle/PLSQL ATAN2 function is:
ATAN2( n, m )
PARAMETERS OR ARGUMENTS
APPLIES TO
The ATAN2 function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ATAN2 function examples and explore how to use the ATAN2
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL AVG function returns the average value of an expression.
SYNTAX
The syntax for the Oracle/PLSQL AVG function is:
FROM tables
WHERE conditions;
PARAMETERS OR ARGUMENTS
APPLIES TO
The AVG function can be used in the following versions of Oracle/PLSQL:
For example, you might wish to know how the average salary of all employees whose salary
is above $25,000 / year.
FROM employees
In this AVG function example, we've aliased the AVG(salary) expression as "Avg Salary". As
a result, "Avg Salary" will display as the field name when the result set is returned.
FROM employees
If there were two salaries of $30,000/year, only one of these values would be used in the
AVG function.
FROM orders;
FROM order_details
GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in
the AVG function, you must use a GROUP BY clause. The department field must, therefore,
be listed in the GROUP BY section.
DESCRIPTION
The Oracle/PLSQL BIN_TO_NUM function converts a bit vector to a number.
SYNTAX
The syntax for the Oracle/PLSQL BIN_TO_NUM function is:
PARAMETERS OR ARGUMENTS
expr1, expr2, ... expr_n must be either 0 or 1 values. They represent bits in a bit vector.
APPLIES TO
The BIN_TO_NUM function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle BIN_TO_NUM function examples and explore how to use the
BIN_TO_NUM function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL BITAND function returns an integer representing an AND operation on
the bits of expr1 and expr2.
SYNTAX
The syntax for the Oracle/PLSQL BITAND function is:
PARAMETERS OR ARGUMENTS
APPLIES TO
The BITAND function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle BITAND function examples and explore how to use the BITAND
function in Oracle/PLSQL.
For example:
BITAND(5,3) would
return
1
BITAND(15,7) would
return
7
BITAND(5,2) would
return
0
BITAND(5,0) would
return
0
BITAND(6,2) would
return
2
DESCRIPTION
The Oracle/PLSQL CEIL function returns the smallest integer value that
is greater than or equal to a number.
SYNTAX
The syntax for the Oracle/PLSQL CEIL function is:
CEIL( number )
PARAMETERS OR ARGUMENTS
number is the value used to find the smallest integer value.
APPLIES TO
The CEIL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle CEIL function examples and explore how to use
the CEIL function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL CORR function returns the coefficient of correlation of
a set of number pairs.
SYNTAX
The syntax for the Oracle/PLSQL CORR function is:
NOTE
The CORR function requires at least two rows.
APPLIES TO
The CORR function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle CORR function examples and explore how to
use the CORR function in Oracle/PLSQL.
For example:
from sales;
from data
group by max_extents;
DESCRIPTION
The Oracle/PLSQL COS function returns the cosine of a number.
SYNTAX
The syntax for the Oracle/PLSQL COS function is:
COS( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The COS function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle COS function examples and explore how to use
the COS function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL COSH function returns the hyperbolic cosine of a
number.
SYNTAX
The syntax for the Oracle/PLSQL COSH function is:
COSH( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The COSH function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle COSH function examples and explore how to
use the COSH function in Oracle/PLSQL.
For example:
COSH(0) 1
DESCRIPTION
The Oracle/PLSQL COUNT function returns the count of an expression.
SYNTAX
The syntax for the Oracle/PLSQL COUNT function is:
FROM tables
WHERE conditions;
PARAMETERS OR ARGUMENTS
1 IBM CA
2 Microsoft
3 NVIDIA
And if you ran the following SELECT statement that uses the COUNT
function:
SELECT COUNT(supplier_id)
FROM suppliers;
This COUNT example will return 3 since all supplier_id values in the
query's result set are NOT NULL.
However, if you ran the next SELECT statement that uses the COUNT
function:
SELECT COUNT(state)
FROM suppliers;
This COUNT example will only return 1, since only one state value in the
query's result set is NOT NULL. That would be the first row where the state
= 'CA'. It is the only row that is included in the COUNT function calculation.
APPLIES TO
The COUNT function can be used in the following versions of
Oracle/PLSQL:
For example, you might wish to know how many employees have a salary
above $75,000 / year.
FROM employees
For example, you could also use the COUNT function to return the name
of the department and the number of employees (in the associated
department) that are in the state of 'CA'.
FROM employees
GROUP BY department;
Because you have listed one column in your SELECT statement that is not
encapsulated in the COUNT function, you must use a GROUP BY clause.
The department field must, therefore, be listed in the GROUP BY section.
ORACLE/PLSQL: COVAR_POP
FUNCTION
Learn how to use the Oracle/PLSQL COVAR_POP function with syntax
and examples.
DESCRIPTION
The Oracle/PLSQL COVAR_POP function returns the population
covariance of a set of number pairs.
SYNTAX
The syntax for the Oracle/PLSQL COVAR_POP function is:
COVAR_POP( expression1, expression2 )
PARAMETERS OR ARGUMENTS
NOTE
The COVAR_POP function eliminates all pairs
where expression1 or expression2 has a null value.
APPLIES TO
The COVAR_POP function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle COVAR_POP function examples and explore
how to use the COVAR_POP function in Oracle/PLSQL.
For example:
from all_tables
group by owner;
ORACLE/PLSQL: COVAR_SAMP
FUNCTION
Learn how to use the Oracle/PLSQL COVAR_SAMP function with syntax
and examples.
DESCRIPTION
The Oracle/PLSQL COVAR_SAMP function returns the sample
covariance of a set of number pairs.
SYNTAX
The syntax for the Oracle/PLSQL COVAR_SAMP function is:
PARAMETERS OR ARGUMENTS
NOTE
The COVAR_SAMP function eliminates all pairs
where expression1 or expression2 has a null value.
APPLIES TO
The COVAR_SAMP function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle COVAR_SAMP function examples and explore
how to use the COVAR_SAMP function in Oracle/PLSQL.
For example:
from all_tables
group by owner;
ORACLE/PLSQL: CUME_DIST
FUNCTION
Learn how to use the Oracle/PLSQL CUME_DIST function with syntax
and examples.
DESCRIPTION
The Oracle/PLSQL CUME_DIST function returns the cumulative
distribution of a value in a group of values. The CUME_DIST function will
return a value that is >0 and <=1.
PARAMETERS OR ARGUMENTS
NOTE
There must be the same number of expressions in the first expression list
as there is in the ORDER BY clause.
APPLIES TO
The CUME_DIST function can be used in the following versions of
Oracle/PLSQL:
For example:
from employees;
APPLIES TO
The CUME_DIST function can be used in the following versions of
Oracle/PLSQL:
For example:
from employees
where department = 'Marketing';
The SQL statement above would return the salary percentile for each
employee in the Marketing department.
ORACLE/PLSQL: DENSE_RANK
FUNCTION
Learn how to use the Oracle/PLSQL DENSE_RANK function with syntax
and examples.
DESCRIPTION
The Oracle/PLSQL DENSE_RANK function returns the rank of a row in a
group of rows. It is very similar to the RANK function. However,
the RANK function can cause non-consecutive rankings if the tested
values are the same. Whereas, the DENSE_RANK function will always
result in consecutive rankings.
PARAMETERS OR ARGUMENTS
NOTE
There must be the same number of expressions in the first
expression list as there is in the ORDER BY clause.
The expression lists match by position so the data types must be
compatible between the expressions in the first expression list as in
the ORDER BY clause.
APPLIES TO
The DENSE_RANK function can be used in the following versions of
Oracle/PLSQL:
For example:
from employees;
The SQL statement above would return the dense rank of an employee
with a salary of $1,000 and a bonus of $500 from within the employees
table.
APPLIES TO
The DENSE_RANK function can be used in the following versions of
Oracle/PLSQL:
For example:
from employees
The SQL statement above would return all employees who work in the
Marketing department and then calculate a rank for each unique salary in
the Marketing department. If two employees had the same salary, the
DENSE_RANK function would return the same rank for both employees.
DESCRIPTION
The Oracle/PLSQL EXP function returns e raised to the nth power, where
e = 2.71828183.
SYNTAX
The syntax for the Oracle/PLSQL EXP function is:
exp( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The EXP function can be used in the following versions of Oracle/PLSQL:
For example:
ORACLE/PLSQL: EXTRACT
FUNCTION
Learn how to use the Oracle/PLSQL EXTRACT function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL EXTRACT function extracts a value from a date or
interval value.
SYNTAX
The syntax for the Oracle/PLSQL EXTRACT function is:
EXTRACT (
| { TIMEZONE_HOUR | TIMEZONE_MINUTE }
| { TIMEZONE_REGION | TIMEZONE_ABBR }
NOTE
You can only extract YEAR, MONTH, and DAY from a DATE.
APPLIES TO
The EXTRACT function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle EXTRACT function examples and explore how
to use the EXTRACT function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL FLOOR function returns the largest integer value that
is equal to or less than a number.
SYNTAX
The syntax for the Oracle/PLSQL FLOOR function is:
FLOOR( number )
PARAMETERS OR ARGUMENTS
number is the value used to determine the largest integer value that is
equal to or less than a number.
APPLIES TO
The FLOOR function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle FLOOR function examples and explore how to
use the FLOOR function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: GREATEST
FUNCTION
Learn how to use the Oracle/PLSQL GREATEST function with syntax
and examples.
DESCRIPTION
The Oracle/PLSQL GREATEST function returns the greatest value in a
list of expressions.
SYNTAX
The syntax for the Oracle/PLSQL GREATEST function is:
PARAMETERS OR ARGUMENTS
NOTE
If the datatypes of the expressions are different, all expressions will be
converted to whatever datatype expr1 is.
APPLIES TO
The GREATEST function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle GREATEST function examples and explore how
to use the GREATEST function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL LEAST function returns the smallest value in a list of
expressions.
SYNTAX
The syntax for the Oracle/PLSQL LEAST function is:
PARAMETERS OR ARGUMENTS
expr1, expr2, . expr_n are expressions that are evaluated by the LEAST
function.
NOTE
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.
Having a NULL value in one of the
expressions will return NULL as the
least value.
APPLIES TO
The LEAST function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LEAST function examples and explore how to
use the LEAST function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: LN FUNCTION
Learn how to use the Oracle/PLSQL LN function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL LN function returns the natural logarithm of a number.
SYNTAX
The syntax for the Oracle/PLSQL LN function is:
LN( number )
PARAMETERS OR ARGUMENTS
APPLIES TO
The LN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LN function examples and explore how to use the LN function in
Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL LOG function returns the logarithm of n base m.
SYNTAX
The syntax for the Oracle/PLSQL LOG function is:
LOG( m, n )
PARAMETERS OR ARGUMENTS
APPLIES TO
The LOG function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
EXAMPLE
Let's look at some Oracle LOG function examples and explore how to use the LOG function
in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL MAX function returns the maximum value of an expression.
SYNTAX
The syntax for the Oracle/PLSQL MAX function is:
FROM tables
WHERE conditions;
PARAMETERS OR ARGUMENTS
APPLIES TO
The MAX function can be used in the following versions of Oracle/PLSQL:
For example, you might wish to know how the maximum salary of all employees.
FROM employees;
In this MAX function example, we've aliased the MAX(salary) expression as "Highest Salary".
As a result, "Highest Salary" will display as the field name when the result set is returned.
For example, you could also use the MAX function to return the name of the department and
the maximum salary in the department.
FROM employees
GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in
the MAX function, you must use a GROUP BY clause. The department field must, therefore,
be listed in the GROUP BY section.
Each time a report is run in Oracle, a record is written to this table noting the above info. What
I am trying to do is pull from this table when the last time each distinct report was run and who
ran it last.
My initial query:
FROM report_history
GROUP BY report_name
runs fine. However, it does not provide the name of the user who ran the report.
Adding user_name to both the select list and to the group by clause returns multiple lines for
each report; the results show the last time each person ran each report in question. (i.e.
User1 ran Report 1 on 01-JUL-03, User2 ran Report1 on 01-AUG-03). I don't want that....I
just want to know who ran a particular report the last time it was run.
Any suggestions?
Answer: This is where things get a bit complicated. The SQL SELECT statement below will
return the results that you want:
FROM report_history
First, we've aliased the first instance of the report_history table as rh.
Second, we've included two components in our FROM clause. The first is the table called
report_history (aliased as rh). The second is a select statement:
FROM report_history
We've aliased the max(report_run_date) as maxdate and we've aliased the entire result set
as maxresults.
Now, that we've created this select statement within our FROM clause, Oracle will let us join
these results against our original report_history table. So we've joined the report_name and
report_run_date fields between the tables called rh and maxresults. This allows us to retrieve
the report_name, max(report_run_date) as well as the user_name.
Question: I need help with a SQL query. I have a table in Oracle called orders which has
the following fields: order_no, customer, and amount.
I need a query that will return the customer who has ordered the highest total amount.
Answer: The following SQL should return the customer with the highest total amount in the
orders table.
SELECT query1.*
FROM orders
FROM orders
This SQL SELECT statement will summarize the total orders for each customer and then
return the customer with the highest total orders. This syntax is optimized for Oracle and may
not work for other database technologies.
Question: I'm trying to retrieve some info from an Oracle database. I've got a table
named Scoring with two fields - Name and Score. What I want to get is the highest score from
the table and the name of the player.
FROM Scoring
I would like to find the customer_id, who has Highest order count.
SELECT MAX(COUNT(*))
FROM CUST_ORDER
GROUP BY CUSTOMER_ID;
This gives me the max Count, But, I can't get the CUSTOMER_ID. Can you help me please?
Answer: The following SQL SELECT statement should return the customer with the highest
order count in the cust_order table.
SELECT query1.*
FROM cust_order
FROM cust_order
This SQL SELECT statement will summarize the total orders for each customer and then
return the customer with the highest order count. This syntax is optimized for Oracle and may
not work for other database technologies.
Question: I'm trying to get the employee with the maximum salary from department 30, but I
need to display the employee's full information. I've tried the following query, but it returns the
result from both department 30 and 80:
SELECT *
FROM employees
FROM employees
WHERE department_id=30);
Answer: The SQL SELECT statement that you have written will first determine the
maximum salary for department 30, but then you select all employees that have this salary. In
your case, you must have 2 employees (one in department 30 and another in department 80)
that have this same salary. You need to make sure that you are refining your query results to
only return employees from department 30.
SELECT *
FROM employees
WHERE department_id=30
FROM employees
WHERE department_id=30);
This will return the employee information for only the employee in department 30 that has the
highest salary.
DESCRIPTION
The Oracle/PLSQL MEDIAN function returns the median of an expression.
SYNTAX
The syntax for the Oracle/PLSQL MEDIAN function is:
PARAMETERS OR ARGUMENTS
NOTE
The MEDIAN function is NEW to Oracle 10! In older versions of Oracle, try using the
PERCENTILE_CONT function to calculate the median value.
APPLIES TO
The MEDIAN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle MEDIAN function examples and explore how to use the MEDIAN
function in Oracle/PLSQL.
For example:
select MEDIAN(salary)
from employees
The SQL statement above would return the median salary for all employees in the Marketing
department.
DESCRIPTION
The Oracle/PLSQL MIN function returns the minimum value of an expression.
SYNTAX
The syntax for the Oracle/PLSQL MIN function is:
SELECT MIN( expression )
FROM tables
WHERE conditions;
PARAMETERS OR ARGUMENTS
APPLIES TO
The MIN function can be used in the following versions of Oracle/PLSQL:
For example, you might wish to know how the minimum salary of all employees.
FROM employees;
In this MIN function example, we've aliased the MIN(salary) expression as "Lowest Salary".
As a result, "Lowest Salary" will display as the field name when the result set is returned.
For example, you could also use the MIN function to return the name of the department and
the minimum salary in the department.
FROM employees
GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in
the MIN function, you must use a GROUP BY clause. The department field must, therefore,
be listed in the GROUP BY section.
ORACLE/PLSQL: MOD FUNCTION
Learn how to use the Oracle/PLSQL MOD function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL MOD function returns the remainder of m divided by n.
SYNTAX
The syntax for the Oracle/PLSQL MOD function is:
MOD( m, n )
PARAMETERS OR ARGUMENTS
m - n * floor(m/n)
NOTE
The MOD function uses the floor function in its formula, whereas the REMAINDER
function uses the ROUND function in its formula.
APPLIES TO
The MOD function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle MOD function examples and explore how to use the MOD function
in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL POWER function returns m raised to the nth power.
SYNTAX
The syntax for the Oracle/PLSQL POWER function is:
POWER( m, n )
PARAMETERS OR ARGUMENTS
m is the base.
n is the exponent.
NOTE
If m is negative, then n must be an integer.
APPLIES TO
The POWER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle POWER function examples and explore how to use the POWER
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL RANK function returns the rank of a value in a group of values. It is very
similar to the DENSE_RANK function. However, the rank function can cause non-
consecutive rankings if the tested values are the same. Whereas, the DENSE_RANK
function will always result in consecutive rankings.
The RANK function can be used two ways - as an Aggregate function or as an Analytic
function.
The syntax for the RANK function when used as an Aggregate function is:
PARAMETERS OR ARGUMENTS
expression1 .. expression_n can be one or more expressions which identify a unique row in
the group.
NOTE
There must be the same number of expressions in the first expression list as there is in the
ORDER BY clause.
The expression lists match by position so the data types must be compatible between the
expressions in the first expression list as in the ORDER BY clause.
APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:
For example:
from employees;
The SQL statement above would return the rank of an employee with a salary of $1,000 and
a bonus of $500 from within the employees table.
The syntax for the RANK function when used as an Analytic function is:
APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:
from employees
The SQL statement above would return all employees who work in the Marketing department
and then calculate a rank for each unique salary in the Marketing department. If two
employees had the same salary, the RANK function would return the same rank for both
employees. However, this will cause a gap in the ranks (ie: non-consecutive ranks). This is
quite different from the DENSE_RANK function which generates consecutive rankings.
DESCRIPTION
The Oracle/PLSQL REMAINDER function returns the remainder of m divided by n.
SYNTAX
The syntax for the Oracle/PLSQL REMAINDER function is:
REMAINDER( m, n )
PARAMETERS OR ARGUMENTS
NOTE
The REMAINDER function uses the round function in its formula, whereas the MOD
function uses the floor function in its formula.
APPLIES TO
The REMAINDER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle REMAINDER function examples and explore how to use the
REMAINDER function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ROUND function returns a number rounded to a certain number of
decimal places.
PARAMETERS OR ARGUMENTS
decimal_places is the number of decimal places rounded to. This value must be an integer. If
this parameter is omitted, the ROUND function will round the number to 0 decimal places.
APPLIES TO
The ROUND function can be used in the following versions of Oracle/PLSQL:
For example:
DESCRIPTION
The Oracle/PLSQL SIGN function returns a value indicating the sign of a number.
SYNTAX
The syntax for the Oracle/PLSQL SIGN function is:
SIGN( number )
PARAMETERS OR ARGUMENTS
number is the number to test for its sign.
NOTE
If number < 0, then sign returns -1.
APPLIES TO
The SIGN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SIGN function examples and explore how to use the SIGN function
in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL SIN function returns the sine of n.
SYNTAX
The syntax for the Oracle/PLSQL SIN function is:
SIN( n )
PARAMETERS OR ARGUMENTS
APPLIES TO
The SIN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SIN function examples and explore how to use the SIN function in
Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL SINH function returns the hyperbolic sine of n.
SYNTAX
The syntax for the Oracle/PLSQL SINH function is:
SINH( n )
PARAMETERS OR ARGUMENTS
n is a number.
APPLIES TO
The SINH function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SINH function examples and explore how to use the SINH function
in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL SQRT function returns the square root of n.
SYNTAX
The syntax for the Oracle/PLSQL SQRT function is:
SQRT( n )
PARAMETERS OR ARGUMENTS
n is a positive number.
APPLIES TO
The SQRT function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SQRT function examples and explore how to use the SQRT
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL STDDEV function returns the standard deviation of a set of numbers.
The STDDEV function can be used two ways - as an Aggregate function or as an Analytic
function.
PARAMETERS OR ARGUMENTS
APPLIES TO
The STDDEV function can be used in the following versions of Oracle/PLSQL:
For example:
select STDDEV(bonus)
from employees;
The SQL statement above would return the standard deviation of the bonus field in the
employees table.
APPLIES TO
The STDDEV function can be used in the following versions of Oracle/PLSQL:
from employees
The SQL statement above would return the cumulative standard deviation of the bonuses in
the Marketing department in the employees table, ordered by salary.
DESCRIPTION
The Oracle/PLSQL SUM function returns the summed value of an expression.
SYNTAX
The syntax for the Oracle/PLSQL SUM function is:
FROM tables
WHERE conditions;
PARAMETERS OR ARGUMENTS
APPLIES TO
The SUM function can be used in the following versions of Oracle/PLSQL:
For example, you might wish to know how the combined total salary of all employees whose
salary is above $50,000 / year.
FROM employees
In this SUM function example, we've aliased the SUM(salary) expression as "Total Salary".
As a result, "Total Salary" will display as the field name when the result set is returned.
FROM employees
If there were two salaries of $82,000/year, only one of these values would be used in the
SUM function.
FROM orders;
For example, you could also use the SUM function to return the name of the department and
the total sales (in the associated department).
FROM order_details
GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in
the SUM function, you must use a GROUP BY clause. The department field must, therefore,
be listed in the GROUP BY section.
DESCRIPTION
The Oracle/PLSQL TAN function returns the tangent of n.
SYNTAX
The syntax for the Oracle/PLSQL TAN function is:
TAN( n )
PARAMETERS OR ARGUMENTS
APPLIES TO
The TAN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TAN function examples and explore how to use the TAN function in
Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TANH function returns the hyperbolic tangent of n.
SYNTAX
The syntax for the Oracle/PLSQL TANH function is:
TANH( n )
PARAMETERS OR ARGUMENTS
n is a number.
APPLIES TO
The TANH function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TANH function examples and explore how to use the TANH
function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_NUMBER function converts a string to a number.
SYNTAX
The syntax for the Oracle/PLSQL TO_NUMBER function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a number.
nls_language is optional. This is the nls language used to convert string1 to a number.
APPLIES TO
The TO_NUMBER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_NUMBER function examples and explore how to use the
TO_NUMBER function in Oracle/PLSQL.
For example:
Since the format_mask and nls_language parameters are optional, you can simply convert a
text string to a numeric value as follows:
DESCRIPTION
The Oracle/PLSQL TRUNC function returns a number truncated to a certain number of
decimal places.
PARAMETERS OR ARGUMENTS
decimal_places is the number of decimal places to truncate to. This value must be an integer.
If this parameter is omitted, the TRUNC function will truncate the number to 0 decimal places.
APPLIES TO
The TRUNC function can be used in the following versions of Oracle/PLSQL:
For example:
DESCRIPTION
The Oracle/PLSQL VARIANCE function returns the variance of a set of numbers.
SYNTAX
The syntax for the Oracle/PLSQL VARIANCE function is:
VARIANCE( expression )
PARAMETERS OR ARGUMENTS
APPLIES TO
The VARIANCE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle VARIANCE function examples and explore how to use the
VARIANCE function in Oracle/PLSQL.
For example:
select VARIANCE(char_length)
from all_tab_columns;
SYNTAX
The syntax for the Oracle/PLSQL VAR_POP function is:
VAR_POP( expression )
PARAMETERS OR ARGUMENTS
NOTE
The VAR_POP function eliminates all null values before performing its calculations.
APPLIES TO
The VAR_POP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle VAR_POP function examples and explore how to use the
VAR_POP function in Oracle/PLSQL.
For example:
select VAR_POP(data_length)
from all_tab_columns;
DESCRIPTION
The Oracle/PLSQL VAR_SAMP function returns the sample variance of a set of numbers.
SYNTAX
The syntax for the Oracle/PLSQL VAR_SAMP function is:
VAR_SAMP( expression )
PARAMETERS OR ARGUMENTS
NOTE
The VAR_SAMP function eliminates all null values before performing its calculations.
APPLIES TO
The VAR_SAMP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle VAR_SAMP function examples and explore how to use the
VAR_SAMP function in Oracle/PLSQL.
For example:
select VAR_SAMP(char_length)
from all_tab_columns;
DESCRIPTION
The Oracle/PLSQL ADD_MONTHS function returns a date plus n months.
SYNTAX
The syntax for the Oracle/PLSQL ADD_MONTHS function is:
ADD_MONTHS( date1, n )
PARAMETERS OR ARGUMENTS
date1 is the starting date (before the n months have been added).
APPLIES TO
The ADD_MONTHS function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle ADD_MONTHS function examples and explore how to use the
ADD_MONTHS function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL CURRENT_DATE function returns the current date in the time zone of
the current SQL session as set by the ALTER SESSION command.
SYNTAX
The syntax for the Oracle/PLSQL CURRENT_DATE function is:
CURRENT_DATE
PARAMETERS OR ARGUMENTS
EXAMPLE
The CURRENT_DATE function can be used in Oracle/PLSQL.
select CURRENT_DATE
from dual;
9/10/2005 10:58:24 PM
You then modified the session time zone with the following ALTER SESSION command:
select CURRENT_DATE
from dual;
9/11/2005 3:58:24 AM
The session time zone value has changed from -7:0 to -2:0, causing the CURRENT_DATE
function to return the current date as a value 5 hours ahead.
ORACLE/PLSQL: CURRENT_TIMESTAMP
FUNCTION
Learn how to use the Oracle/PLSQL CURRENT_TIMESTAMP function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL CURRENT_TIMESTAMP function returns the current date and time in
the time zone of the current SQL session as set by the ALTER SESSION command. It returns
a TIMESTAMP WITH TIME ZONE value.
SYNTAX
The syntax for the Oracle/PLSQL CURRENT_TIMESTAMP function is:
CURRENT_TIMESTAMP
PARAMETERS OR ARGUMENTS
NOTE
A similar function to the CURRENT_TIMESTAMP function is the LOCALTIMESTAMP
function.
The difference between these two functions is that the CURRENT_TIMESTAMP
function returns a TIMESTAMP WITH TIME ZONE value while the LOCALTIMESTAMP
function returns a TIMESTAMP value.
APPLIES TO
The CURRENT_TIMESTAMP function can be used in the following versions of
Oracle/PLSQL:
EXAMPLE
The CURRENT_TIMESTAMP function can be used in Oracle/PLSQL.
select CURRENT_TIMESTAMP
from dual;
You then modified the session time zone with the following ALTER SESSION command:
select CURRENT_TIMESTAMP
from dual;
The session time zone value has changed from -7:0 to -2:0, causing the
CURRENT_TIMESTAMP function to return the current date and time as a value 5 hours
ahead.
DESCRIPTION
The Oracle/PLSQL DBTIMEZONE function returns the database time zone as a time zone
offset (in the following format: '[+|-]TZH:TZM') or a time zone region name.
SYNTAX
The syntax for the Oracle/PLSQL DBTIMEZONE function is:
DBTIMEZONE
PARAMETERS OR ARGUMENTS
APPLIES TO
The DBTIMEZONE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle DBTIMEZONE function examples and explore how to use the
DBTIMEZONE function in Oracle/PLSQL.
For example:
select DBTIMEZONE
from dual;
+00:00
-07:00
The value returned depends on the time zone value set in your individual Oracle database.
DESCRIPTION
The Oracle/PLSQL FROM_TZ function converts a TIMESTAMP value (given a TIME ZONE)
to a TIMESTAMP WITH TIME ZONE value.
SYNTAX
The syntax for the Oracle/PLSQL FROM_TZ function is:
PARAMETERS OR ARGUMENTS
EXAMPLE
Let's look at some Oracle FROM_TZ function examples and explore how to use the
FROM_TZ function in Oracle/PLSQL.
For example:
from dual;
If you executed the same SQL statement, but set the time zone to '-7:00' as follows:
from dual;
DESCRIPTION
The Oracle/PLSQL LAST_DAY function returns the last day of the month based on
a date value.
SYNTAX
The syntax for the Oracle/PLSQL LAST_DAY function is:
LAST_DAY( date )
PARAMETERS OR ARGUMENTS
date is the date value to use to calculate the last day of the month.
APPLIES TO
The LAST_DAY function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LAST_DAY function examples and explore how to use the
LAST_DAY function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: LOCALTIMESTAMP
FUNCTION
Learn how to use the Oracle/PLSQL LOCALTIMESTAMP function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL LOCALTIMESTAMP function returns the current date and time in the
time zone of the current SQL session as set by the ALTER SESSION command. It returns a
TIMESTAMP value.
SYNTAX
The syntax for the Oracle/PLSQL LOCALTIMESTAMP function is:
LOCALTIMESTAMP
PARAMETERS OR ARGUMENTS
NOTE
A similar function to the LOCALTIMESTAMP function is the CURRENT_TIMESTAMP
function.
The difference between these two functions is that the LOCALTIMESTAMP function
returns a TIMESTAMP value while the CURRENT_TIMESTAMP function returns a
TIMESTAMP WITH TIME ZONE value.
APPLIES TO
The LOCALTIMESTAMP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle LOCALTIMESTAMP function examples and explore how to use the
LOCALTIMESTAMP function in Oracle/PLSQL.
For example:
select LOCALTIMESTAMP
from dual;
10-Sep-05 10.58.24 PM
You then modified the session time zone with the following ALTER SESSION command:
from dual;
10-Sep-05 03.58.24 AM
The session time zone value has changed from -7:0 to -2:0, causing the LOCALTIMESTAMP
function to return the current date and time as a value 5 hours ahead.
ORACLE/PLSQL: MONTHS_BETWEEN
FUNCTION
Learn how to use the Oracle/PLSQL MONTHS_BETWEEN function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL MONTHS_BETWEEN function returns the number of months
between date1 and date2.
SYNTAX
The syntax for the Oracle/PLSQL MONTHS_BETWEEN function is:
PARAMETERS OR ARGUMENTS
date1 and date2 are the dates used to calculate the number of months.
APPLIES TO
The MONTHS_BETWEEN function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle MONTHS_BETWEEN function examples and explore how to use
the MONTHS_BETWEEN function in Oracle/PLSQL.
For example:
would return 0
would return 2
DESCRIPTION
The Oracle/PLSQL NEW_TIME function returns a date in time zone1 to a date in
time zone2.
SYNTAX
The syntax for the Oracle/PLSQL NEW_TIME function is:
PARAMETERS OR ARGUMENTS
zone1 and zone2 can be any of the following values:
Value Description
APPLIES TO
The NEW_TIME function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NEW_TIME function examples and explore how to use the
NEW_TIME function in Oracle/PLSQL.
For example:
The following NEW_TIME function example converts an Atlantic Standard Time into a
Mountain Standard Time:
DESCRIPTION
The Oracle/PLSQL NEXT_DAY function returns the first weekday that is greater than a date.
SYNTAX
The syntax for the Oracle/PLSQL NEXT_DAY function is:
PARAMETERS OR ARGUMENTS
date is used to find the next weekday.
APPLIES TO
The NEXT_DAY function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle NEXT_DAY function examples and explore how to use the
NEXT_DAY function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL ROUND function returns a date rounded to a specific unit of measure.
PARAMETERS OR ARGUMENTS
Month MONTH, MON, MM, RM Rounds up on the 16th day of the month
Minute MI
APPLIES TO
The ROUND function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
For example:
ORACLE/PLSQL: SESSIONTIMEZONE
FUNCTION
Learn how to use the Oracle/PLSQL SESSIONTIMEZONE function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL SESSIONTIMEZONE function returns the current session's time zone as
a time zone offset (in the following format: '[+|-]TZH:TZM') or a time zone region name.
SYNTAX
The syntax for the Oracle/PLSQL SESSIONTIMEZONE function is:
SESSIONTIMEZONE
PARAMETERS OR ARGUMENTS
APPLIES TO
The SESSIONTIMEZONE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SESSIONTIMEZONE function examples and explore how to use
the SESSIONTIMEZONE function in Oracle/PLSQL.
For example:
SELECT SESSIONTIMEZONE
FROM dual;
+00:00
-07:00
The value returned depends on how the user specified the session time zone value in the
most recent ALTER SESSION statement. An example of an ALTER SESSION command is:
DESCRIPTION
The Oracle/PLSQL SYSDATE function returns the current system date and time on your
local database.
SYNTAX
The syntax for the Oracle/PLSQL SYSDATE function is:
SYSDATE
PARAMETERS OR ARGUMENTS
There are no parameters or arguments for the SYSDATE function.
APPLIES TO
The SYSDATE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SYSDATE function examples and explore how to use the
SYSDATE function in Oracle/PLSQL.
For example:
select SYSDATE
into v_date
from dual;
The variable called v_date will now contain the current date and time value.
You could also use the SYSDATE function in any SQL statement. For example:
from suppliers
If you wanted to extract the date portion only (and exclude the time component), you could
use the TO_CHAR function. For example:
from suppliers
DESCRIPTION
The Oracle/PLSQL SYSTIMESTAMP function returns the current system date and time
(including fractional seconds and time zone) on your local database.
SYNTAX
The syntax for the Oracle/PLSQL SYSTIMESTAMP function is:
SYSTIMESTAMP
PARAMETERS OR ARGUMENTS
APPLIES TO
The SYSTIMESTAMP function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle SYSTIMESTAMP function examples and explore how to use the
SYSTIMESTAMP function in Oracle/PLSQL.
For example:
SELECT SYSTIMESTAMP
FROM dual;
03-11-21 20:4
You can also choose to use the TO_CHAR function with the SYSTIMESTAMP function. For
example:
FROM dual;
4141.550774
ORACLE/PLSQL: TO_CHAR FUNCTION
Learn how to use the Oracle/PLSQL TO_CHAR function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL TO_CHAR function converts a number or date to a string.
SYNTAX
The syntax for the Oracle/PLSQL TO_CHAR function is:
PARAMETERS OR ARGUMENTS
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.
APPLIES TO
The TO_CHAR function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_CHAR function examples and explore how to use the
TO_CHAR function in Oracle/PLSQL.
WITH NUMBERS
For example:
WITH DATES
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
W Week of month (1-5) where week 1 starts on the first day of the month
and ends on the seventh.
MI Minute (0-59).
SS Second (0-59).
FF Fractional seconds.
You will notice that in some TO_CHAR function examples, the format_mask parameter
begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the
examples below.
The zeros have been suppressed so that the day component shows as "9" as opposed to
"09".
ORDER BY "Day";
Answer: In the above SQL, the fmDay format mask used in the TO_CHAR function will
return the name of the Day and not the numeric value of the day.
To sort the days of the week in order, you need to return the numeric value of the day by
using the fmD format mask as follows:
FROM emp
ORDER BY "Day";
DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.
SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a date.
nls_language is optional. This is the nls language used to convert string1 to a date.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
YEAR Year, spelled out
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.
MI Minute (0-59).
SS Second (0-59).
AD or A.D AD indicator
BC or B.C. BC indicator
TZD Daylight savings information. For example, 'PST'
APPLIES TO
The TO_DATE function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.
For example:
DESCRIPTION
The Oracle/PLSQL TO_TIMESTAMP function converts a string to a timestamp.
SYNTAX
The syntax for the Oracle/PLSQL TO_TIMESTAMP function is:
PARAMETERS OR ARGUMENTS
format_mask is optional. This is the format that will be used to convert string1 to a timestamp.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
MI Minute (0-59).
SS Second (0-59).
APPLIES TO
The TO_TIMESTAMP function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
EXAMPLE
Let's look at some Oracle TO_TIMESTAMP function examples and explore how to use the
TO_TIMESTAMP function in Oracle/PLSQL.
For example:
ORACLE/PLSQL: TO_TIMESTAMP_TZ
FUNCTION
Learn how to use the Oracle/PLSQL TO_TIMESTAMP_TZ function with syntax and
examples.
DESCRIPTION
The Oracle/PLSQL TO_TIMESTAMP_TZ function converts a string to a timestamp with time
zone.
SYNTAX
The syntax for the Oracle/PLSQL TO_TIMESTAMP_TZ function is:
PARAMETERS OR ARGUMENTS
string1 is the string that will be converted to a timestamp with time zone.
format_mask is optional. This is the format that will be used to convert string1 to a timestamp
with time zone.
The following is a list of options for the format_mask parameter. These parameters can be
used in many combinations.
Parameter Explanation
MI Minute (0-59).
SS Second (0-59).
APPLIES TO
The TO_TIMESTAMP_TZ function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_TIMESTAMP_TZ function examples and explore how to use
the TO_TIMESTAMP_TZ function in Oracle/PLSQL.
For example:
TO_TIMESTAMP_TZ('2003/12/13 10:13:18 -8:00', 'YYYY/MM/DD HH:MI:SS TZH:TZM')
would also return '13-DEC-03 10.13.18.000000000 AM -08:00' as a timestamp with time zone
value.
DESCRIPTION
The Oracle/PLSQL TO_YMINTERVAL function converts a string to an INTERVAL YEAR TO
MONTH type.
SYNTAX
The syntax for the Oracle/PLSQL TO_YMINTERVAL function is:
TO_YMINTERVAL( character )
PARAMETERS OR ARGUMENTS
character is the value to convert to an INTERVAL YEAR TO MONTH type. It can be a char,
varchar2, nchar, or nvarchar2 value.
APPLIES TO
The TO_YMINTERVAL function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TO_YMINTERVAL function examples and explore how to use the
TO_YMINTERVAL function in Oracle/PLSQL.
For example:
The TO_YMINTERVAL function is most commonly used to add an interval to a date field. For
example, you may wish to add 1 year and 5 months to an order date.
from orders;
This SQL statement would return the order date, as well as the order date plus 1 year and 5
months
DESCRIPTION
The Oracle/PLSQL TRUNC function returns a date truncated to a specific unit of measure.
PARAMETERS OR ARGUMENTS
format is the unit of measure to apply for truncating. If the format parameter is omitted, the
TRUNC function will truncate the date to the day value, so that any hours, minutes, or
seconds will be truncated off.
Quarter Q
Week WW
IW IW
W W
Minute MI
APPLIES TO
The TRUNC function can be used in the following versions of Oracle/PLSQL:
For example:
TRUNC(TO_DATE('22-AUG-03'), 'YEAR') would return '01-JAN-03'
DESCRIPTION
The Oracle/PLSQL TZ_OFFSET function returns the time zone offset of a value.
SYNTAX
The syntax for the Oracle/PLSQL TZ_OFFSET function is:
TZ_OFFSET( timezone )
PARAMETERS OR ARGUMENTS
timezone can be a valid time zone name, a time zone offset from UTC,
the SESSIONTIMEZONE function, or the DBTIMEZONE function.
Canada/Atlantic Pacific/Easter
Canada/Central Pacific/Honolulu
Canada/East-Saskatchewan Pacific/Kwajalein
Canada/Eastern Pacific/Pago_Pago
Canada/Mountain Pacific/Samoa
Canada/Newfoundland US/Alaska
Canada/Pacific US/Aleutian
Canada/Saskatchewan US/Arizona
Canada/Yukon US/Central
Europe/Dublin US/East-Indiana
Europe/Istanbul US/Eastern
Europe/Lisbon US/Hawaii
Europe/London US/Michigan
Europe/Moscow US/Mountain
Europe/Warsaw US/Pacific
Greenwich US/Pacific-New
Pacific/Auckland US/Samoa
Pacific/Chatham
To find more time zone values, you can run the following query:
FROM V$TIMEZONE_NAMES;
APPLIES TO
The TZ_OFFSET function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle TZ_OFFSET function examples and explore how to use the
TZ_OFFSET function in Oracle/PLSQL.
For example:
MISCELLANEOUS FUNCTIONS
SYNTAX
The syntax for the Oracle/PLSQL SQLCODE function is:
SQLCODE
PARAMETERS OR ARGUMENTS
There are no parameters or arguments for the SQLCODE function.
NOTE
See also the SQLERRM function.
EXAMPLE
Since EXCEPTION HANDLING is usually written with the following syntax:
EXCEPTION
[statements]
[statements]
[statements]
[statements]
END [procedure_name];
EXCEPTION
Or you could log the error to a table using the SQLCODE function as follows:
EXCEPTION
err_code := SQLCODE;
END;
SYNTAX
The syntax for the Oracle/PLSQL SQLERRM function is:
SQLERRM
PARAMETERS OR ARGUMENTS
NOTE
See also the SQLCODE function.
EXAMPLE
Since EXCEPTION HANDLING is usually written with the following syntax:
EXCEPTION
[statements]
[statements]
[statements]
[statements]
END [procedure_name];
EXCEPTION
END;
Or you could log the error to a table using the SQLERRM function as follows:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
END;
DESCRIPTION
The Oracle/PLSQL USER function returns the user_id from the current Oracle session.
SYNTAX
The syntax for the Oracle/PLSQL USER function is:
USER
PARAMETERS OR ARGUMENTS
APPLIES TO
The USER function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle USER function examples and explore how to use the USER
function in Oracle/PLSQL.
For example:
parm_user_ID := USER;
OR
select USER
into parm_user_ID
from dual;
The variable called parm_user_ID will now contain the Oracle user ID from the current Oracle
session.
DESCRIPTION
The Oracle/PLSQL UID function returns the id number for a user's session (the user who is
currently logged in).
SYNTAX
The syntax for the Oracle/PLSQL UID function is:
UID
PARAMETERS OR ARGUMENTS
APPLIES TO
The UID function can be used in the following versions of Oracle/PLSQL:
EXAMPLE
Let's look at some Oracle UID function examples and explore how to use the UID function in
Oracle/PLSQL.
For example:
SELECT UID
INTO parm_uid
FROM dual;
The variable called parm_uid will now contain the id number for the user's session. The value
could be 25, for example.