Вы находитесь на странице: 1из 219

ASCII FUNCTION

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle ASCII function examples and explore how to use the ASCII function in
Oracle/PLSQL.

For example:

ASCII('t') would return 116

ASCII('T') would return 84

ASCII('T2') would also return 84


ASCIISTR FUNCTION
Learn how to use the Oracle/PLSQL ASCIISTR function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE
Let's look at some Oracle ASCIISTR function examples and explore how to use the ASCIISTR function in
Oracle/PLSQL.

For example:

ASCIISTR('A B C ') would return 'A B C \00C4 \00CA'

ASCIISTR('A B C ') would return 'A B C \00D5 \00D8'

ASCIISTR('A B C would return 'A B C \00C4 \00CA \00CD \00D5


') \00D8'
CHR FUNCTION
Learn how to use the Oracle/PLSQL CHR function with syntax and examples.

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

number_code is the NUMBER code used to retrieve the character.

APPLIES TO
The CHR 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 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.

Unistring Value Resulting character

unistr('\0300') grave accent ( ` )

unistr('\0301') acute accent ( )

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:

COMPOSE('o' || unistr('\0308') ) would return

COMPOSE('a' || unistr('\0302') ) would return

COMPOSE('e' || unistr('\0301') ) would return

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:

CONCAT( string1, string2 )

PARAMETERS OR ARGUMENTS

string1 is the first string to concatenate.

string2 is the second string to concatenate.

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'

CONCAT('a', 'b') would return 'ab'

FREQUENTLY ASKED QUESTIONS


Question: How can you use the CONCAT function to concatenate more than 2 strings
together?

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:

CONCAT( CONCAT( string1, string2 ), string3 )

Or you could nest the CONCAT function as follows, if you wanted to concatenate 4 strings:

CONCAT( CONCAT( CONCAT( string1, string2 ), string3 ), string4 )

Here is an example provided by Ruth that demonstrates how to nest multiple CONCAT
functions to concatenate 6 strings:

CONCAT( CONCAT( CONCAT( CONCAT( CONCAT(

'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:

'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column ||


' and a '

|| 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:

string1 || string2 || string_n

PARAMETERS OR ARGUMENTS

string1 is the first string to concatenate.

string2 is the second string to concatenate.

string_n is the nth string to concatenate.

APPLIES TO
The || operator 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 || operator examples and explore how to use the || operator in
Oracle/PLSQL.

For example:

'Tech on' || ' the Net' would return 'Tech on the Net'

'a' || 'b' || 'c' || 'd' would return 'abcd'


CONVERT FUNCTION
Learn how to use the Oracle/PLSQL CONVERT function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL CONVERT function converts a string from one character set to another.

SYNTAX
The syntax of the CONVERT function is:

CONVERT( string1 , char_set_to , [ char_set_from ] )

PARAMETERS OR ARGUMENTS

string1 is the string to be converted.

char_set_to is the character set to convert to.

char_set_from is the character set to convert from.

NOTE
Character sets include:

Character Set Description

US7ASCII US 7-bit ASCII character set

WE8DEC West European 8-bit character set

WE8HP HP West European Laserjet 8-bit character set

F7DEC DEC French 7-bit character set

WE8EBCDIC500 IBM West European EBCDIC Code Page 500

WE8PC850 IBM PC Code Page 850


WE8ISO8859P1 ISO 8859-1 West European 8-bit character set

APPLIES TO
The CONVERT 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 CONVERT function examples and explore how to use the
CONVERT function in Oracle/PLSQL.

For example:

CONVERT('A B C D E ', would return 'A B C


'US7ASCII', 'WE8ISO8859P1') D E A E I ? ?'

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

string is the string that will be decomposed.

APPLIES TO
The DECOMPOSE 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 DECOMPOSE function examples and explore how to use the
DECOMPOSE function in Oracle/PLSQL.

For example:

DECOMPOSE('Trs bien') would return 'Tre`s bien'

DECOMPOSE('') would return 'e'

DECOMPOSE('ol') would return 'ole'

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:

DUMP( expression, [return_format], [start_position], [length] )

PARAMETERS OR ARGUMENTS

expression is the expression to analyze.

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

1008 octal notation with the character set name

1010 decimal notation with the character set name

1016 hexadecimal notation with the character set name

1017 single characters with the character set name

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle DUMP function examples and explore how to use the DUMP
function in Oracle/PLSQL.

For example:

DUMP('Tech') would return 'Typ=96 Len=4: 84,101,99,104'

DUMP('Tech', would return 'Typ=96 Len=4: 84,101,99,104'


10)

DUMP('Tech', would return 'Typ=96 Len=4: 54,65,63,68'


16)

DUMP('Tech', would return 'Typ=96 Len=4


1016) CharacterSet=US7ASCII: 54,65,63,68'
DUMP('Tech', would return 'Typ=96 Len=4
1017) CharacterSet=US7ASCII: T,e,c,h'

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle INITCAP function examples and explore how to use the INITCAP
function in Oracle/PLSQL.

For example:

INITCAP('tech on the net'); would return 'Tech On The Net'

INITCAP('GEORGE BURNS'); would return 'George Burns'

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:

INSTR( string, substring [, start_position [, nth_appearance ] ] )

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.

nth_appearance is the nth appearance of substring. This is optional. If omitted, it defaults to


1.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle INSTR function examples and explore how to use the INSTR
function in Oracle/PLSQL.

For example:

INSTR('Tech on the net', 'e') would return 2; the first occurrence


of 'e'

INSTR('Tech on the net', 'e', would return 2; the first occurrence


1, 1) of 'e'

INSTR('Tech on the net', 'e', would return 11; the second


1, 2) occurrence of 'e'

INSTR('Tech on the net', 'e', would return 14; the third


1, 3) occurrence of 'e'

INSTR('Tech on the net', 'e', would return 2


-3, 2)

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:

INSTR2( string, substring [, start_position [, nth_appearance ] ] )

PARAMETERS OR ARGUMENTS

string is the string to search. string can be CHAR, VARCHAR2, NCHAR, or


NVARCHAR2. string can not be 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
INSTR2 function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.

nth_appearance is the nth appearance of substring. This is optional. If omitted, it defaults to


1.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle INSTR2 function examples and explore how to use the INSTR2
function in Oracle/PLSQL.

For example:

INSTR2('TechOnTheNet.com', 'e') would return 2; the first


occurrence of 'e'

INSTR2('TechOnTheNet.com', 'e', would return 2; the first


1, 1) occurrence of 'e'

INSTR2('TechOnTheNet.com', 'e', would return 9; the second


1, 2) occurrence of 'e'

INSTR2('TechOnTheNet.com', 'e', would return 11; the third


1, 3) occurrence of 'e'

INSTR2('TechOnTheNet.com', 'e', - would return 9


3, 2)

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:

INSTR4( string, substring [, start_position [, nth_appearance ] ] )

PARAMETERS OR ARGUMENTS

string is the string to search. string can be CHAR, VARCHAR2, NCHAR, or


NVARCHAR2. string can not be 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
INSTR4 function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.

nth_appearance is the nth appearance of substring. This is optional. If omitted, it defaults to


1.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle INSTR4 function examples and explore how to use the INSTR4
function in Oracle/PLSQL.

For example:

INSTR4('TechOnTheNet.com', 'e') would return 2; the first


occurrence of 'e'

INSTR4('TechOnTheNet.com', 'e', would return 2; the first


1, 1) occurrence of 'e'

INSTR4('TechOnTheNet.com', 'e', would return 9; the second


1, 2) occurrence of 'e'

INSTR4('TechOnTheNet.com', 'e', would return 11; the third


1, 3) occurrence of 'e'

INSTR4('TechOnTheNet.com', 'e', - would return 9


3, 2)

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:

INSTRB( string, substring [, start_position [, nth_appearance ] ] )

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.

nth_appearance is the nth appearance of substring. This is optional. If omitted, it defaults to


1.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle INSTRB function examples and explore how to use the INSTRB
function in Oracle/PLSQL.

For example:

INSTRB('TechOnTheNet.com', 'e') would return 2; the first


occurrence of 'e'
(single-byte character set)

INSTRB('TechOnTheNet.com', 'e', would return 2; the first


1, 1) occurrence of 'e'
(single-byte character set)

INSTRB('TechOnTheNet.com', 'e', would return 9; the second


1, 2) occurrence of 'e'
(single-byte character set)

INSTRB('TechOnTheNet.com', 'e', would return 11; the third


1, 3) occurrence of 'e'
(single-byte character set)

INSTRB('TechOnTheNet.com', 'e', - would return 9


3, 2) (single-byte character set)

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

string is the string to search. string can be CHAR, VARCHAR2, NCHAR, or


NVARCHAR2. string can not be 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
INSTRC function counts back start_position number of characters from the end of string and
then searches towards the beginning of string.

nth_appearance is the nth appearance of substring. This is optional. If omitted, it defaults to


1.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle INSTRC function examples and explore how to use the INSTRC
function in Oracle/PLSQL.

For example:

INSTRC('TechOnTheNet.com', 'e') would return 2; the first


occurrence of 'e'

INSTRC('TechOnTheNet.com', 'e', would return 2; the first


1, 1) occurrence of 'e'

INSTRC('TechOnTheNet.com', 'e', would return 9; the second


1, 2) occurrence of 'e'

INSTRC('TechOnTheNet.com', 'e', would return 11; the third


1, 3) occurrence of 'e'

INSTRC('TechOnTheNet.com', 'e', - would return 9


3, 2)

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

string1 is the string to return the length for.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LENGTH function examples and explore how to use the LENGTH
function in Oracle/PLSQL.

For example:

LENGTH(NULL) would return NULL


LENGTH('') would return NULL

LENGTH(' ') would return 1

LENGTH('Tech on the Net') would return 15

LENGTH('Tech on the Net ') would return 16

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LENGTH2 function examples and explore how to use the
LENGTH2 function in Oracle/PLSQL.
For example:

LENGTH2(NULL) would return NULL

LENGTH2('') would return NULL

LENGTH2(' ') would return 1

LENGTH2('TechOnTheNet.com') would return 16

LENGTH2('TechOnTheNet.com ') would return 17

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i


EXAMPLE
Let's look at some Oracle LENGTH4 function examples and explore how to use the
LENGTH4 function in Oracle/PLSQL.

For example:

LENGTH4(NULL) would return NULL

LENGTH4('') would return NULL

LENGTH4(' ') would return 1

LENGTH4('TechOnTheNet.com') would return 16

LENGTH4('TechOnTheNet.com ') would return 17

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LENGTHB function examples and explore how to use the
LENGTHB function in Oracle/PLSQL.

For example:

LENGTHB(NULL) would return NULL


(single-byte character set)

LENGTHB('') would return NULL


(single-byte character set)

LENGTHB(' ') would return 1


(single-byte character set)

LENGTHB('TechOnTheNet.com') would return 16


(single-byte character set)

LENGTHB('TechOnTheNet.com ') would return 17


(single-byte character set)

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LENGTHC function examples and explore how to use the
LENGTHC function in Oracle/PLSQL.

For example:

LENGTHC(NULL) would return NULL

LENGTHC('') would return NULL

LENGTHC(' ') would return 1

LENGTHC('TechOnTheNet.com') would return 16

LENGTHC('TechOnTheNet.com ') would return 17

ORACLE/PLSQL: LOWER FUNCTION


Learn how to use the Oracle/PLSQL LOWER function with syntax and examples.

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

string1 is the string to convert to lowercase.

NOTE
See also the UPPER function.

APPLIES TO
The LOWER 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 LOWER function examples and explore how to use the LOWER
function in Oracle/PLSQL.

For example:

LOWER('Tech on the Net'); would return 'tech on the net'

LOWER('GEORGE BURNS 123 '); would return 'george burns 123 '

ORACLE/PLSQL: LPAD FUNCTION


Learn how to use the Oracle/PLSQL LPAD function with syntax and examples.

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

string1 is the string to pad characters to (the left-hand side).

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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', 7); would return ' tech'

LPAD('tech', 2); would return 'te'

LPAD('tech', 8, '0'); would return '0000tech'

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'

ORACLE/PLSQL: LTRIM FUNCTION


Learn how to use the Oracle/PLSQL LTRIM function with syntax and examples.

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:

LTRIM( string1, [ trim_string ] )

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LTRIM function examples and explore how to use the LTRIM
function in Oracle/PLSQL.

For example:

LTRIM(' tech'); would return 'tech'

LTRIM(' tech', ' '); would return 'tech'

LTRIM('000123', '0'); would return '123'

LTRIM('123123Tech', '123'); would return 'Tech'

LTRIM('123123Tech123', '123'); would return 'Tech123'

LTRIM('xyxzyyyTech', 'xyz'); would return 'Tech'


LTRIM('6372Tech', '0123456789'); would return 'Tech'

The LTRIM function may appear to remove patterns, but this is not the case as demonstrated
in the following example.

LTRIM('xxyyxzyxyyxTech', 'xyz'); would return 'Tech'

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.

LTRIM( '637Tech', '0123456789'); would return 'Tech'

In this example, every number combination from 0 to 9 has been listed in


the trim_string parameter. By doing this, it does not matter the order that the numbers appear
in string1, all leading numbers will be removed by the LTRIM function.

ORACLE/PLSQL: NCHR FUNCTION


Learn how to use the Oracle/PLSQL NCHR function with syntax and examples.

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:

CHR ( number_code USING NCHAR_CS )

APPLIES TO
The NCHR 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 NCHR function examples and explore how to use the NCHR
function in Oracle/PLSQL.

For example:

NCHR(116) would return 't'

NCHR(84) would return 'T'

ORACLE/PLSQL: REPLACE FUNCTION


Learn how to use the Oracle/PLSQL REPLACE function with syntax and examples.

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:

REPLACE( string1, string_to_replace, [ replacement_string ] )

PARAMETERS OR ARGUMENTS

string1 is the string to replace a sequence of characters with another set of characters.

string_to_replace is the string that will be searched for in string1.

replacement_string is optional. All occurrences of string_to_replace will be replaced


with replacement_string in string1. If the replacement_string parameter is omitted, the
REPLACE function simply removes all occurrences of string_to_replace, and returns the
resulting string.

APPLIES TO
The REPLACE 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 REPLACE function examples and explore how to use the
REPLACE function in Oracle/PLSQL.

For example:

REPLACE('123123tech', '123'); would return 'tech'

REPLACE('123tech123', '123'); would return 'tech'

REPLACE('222tech', '2', '3'); would return '333tech'

REPLACE('0000123', '0'); would return '123'

REPLACE('0000123', '0', ' '); would return ' 123'

ORACLE/PLSQL: RPAD FUNCTION


Learn how to use the Oracle/PLSQL RPAD function with syntax and examples.

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:

RPAD( string1, padded_length, [ pad_string ] )

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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', 7); would return 'tech '

RPAD('tech', 2); would return 'te'

RPAD('tech', 8, '0'); would return 'tech0000'

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'

ORACLE/PLSQL: RTRIM FUNCTION


Learn how to use the Oracle/PLSQL RTRIM function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle RTRIM function examples and explore how to use the RTRIM
function in Oracle/PLSQL.

For example:

RTRIM('tech '); would return 'tech'

RTRIM('tech ', ' '); would return 'tech'

RTRIM('123000', '0'); would return '123'

RTRIM('Tech123123', '123'); would return 'Tech'

RTRIM('123Tech123', '123'); would return '123Tech'

RTRIM('Techxyxzyyy', 'xyz'); would return 'Tech'

RTRIM('Tech6372', '0123456789'); would return 'Tech'

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.

RTRIM('Tech6372', '0123456789'); would return 'Tech'

In this example, every number combination from 0 to 9 has been listed in


the trim_string parameter. By doing this, it does not matter the order that the numbers appear
in string1, all trailing numbers will be removed by the RTRIM function.

ORACLE/PLSQL: SOUNDEX FUNCTION


Learn how to use the Oracle/PLSQL SOUNDEX function with syntax and examples.

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

string1 is the string whose phonetic value will be returned.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle SOUNDEX function examples and explore how to use the
SOUNDEX function in Oracle/PLSQL.

For example:

SOUNDEX('tech on the net'); would return 'T253'

SOUNDEX('TECH ON THE NET'); would return 'T253'

SOUNDEX('apples'); would return 'A142'

SOUNDEX('apples are great'); would return 'A142'

SOUNDEX('applus'); would return 'A142'

ORACLE/PLSQL: SUBSTR FUNCTION


Learn how to use the Oracle/PLSQL SUBSTR function with syntax and examples.

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:

SUBSTR( string, start_position, [ length ] )

PARAMETERS OR ARGUMENTS

string is the source string.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle SUBSTR function examples and explore how to use the SUBSTR
function in Oracle/PLSQL.

For example:

SUBSTR('This is a test', 6, 2) would return 'is'

SUBSTR('This is a test', 6) would return 'is a test'

SUBSTR('TechOnTheNet', 1, 4) would return 'Tech'

SUBSTR('TechOnTheNet', -3, 3) would return 'Net'

SUBSTR('TechOnTheNet', -6, 3) would return 'The'

SUBSTR('TechOnTheNet', -8, 2) would return 'On'

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:

TO_CHAR( value, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.

nls_language is optional. This is the nls language used to convert value to a string.

APPLIES TO
The TO_CHAR 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 TO_CHAR function examples and explore how to use the
TO_CHAR function in Oracle/PLSQL.

WITH NUMBERS

For example:

The following are number examples for the TO_CHAR function.

TO_CHAR(1210.73, '9999.9') would return ' 1210.7'

TO_CHAR(-1210.73, '9999.9') would return '-1210.7'

TO_CHAR(1210.73, '9,999.99') would return ' 1,210.73'

TO_CHAR(1210.73, '$9,999.00') would return ' $1,210.73'

TO_CHAR(21, '000099') would return ' 000021'


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

YEAR Year, spelled out

YYYY 4-digit year

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the year
and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the month
and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds.

The following are date examples for the TO_CHAR function.

TO_CHAR(sysdate, 'yyyy/mm/dd') would return '2003/07/09'

TO_CHAR(sysdate, 'Month DD, would return 'July 09, 2003'


YYYY')
TO_CHAR(sysdate, 'FMMonth DD, would return 'July 9, 2003'
YYYY')

TO_CHAR(sysdate, 'MON DDth, would return 'JUL 09TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMON DDth, would return 'JUL 9TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMon ddth, would return 'Jul 9th, 2003'


YYYY')

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.

TO_CHAR(sysdate, 'FMMonth DD, would return 'July 9, 2003'


YYYY')

TO_CHAR(sysdate, 'FMMON DDth, would return 'JUL 9TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMon ddth, would return 'Jul 9th, 2003'


YYYY')

The zeros have been suppressed so that the day component shows as "9" as opposed to
"09".

FREQUENTLY ASKED QUESTIONS


Question: Why doesn't this sort the days of the week in order?

SELECT ename, hiredate, TO_CHAR((hiredate),'fmDay') "Day"

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:

SELECT ename, hiredate, TO_CHAR((hiredate),'fmD') "Day"

FROM emp

ORDER BY "Day";

ORACLE/PLSQL: TO_DATE FUNCTION


Learn how to use the Oracle/PLSQL TO_DATE function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.

SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:

TO_DATE( string1, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a date.

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

YYYY 4-digit year


YYY Last 3, 2, or 1 digit(s) of year.
YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

RRRR Accepts a 2-digit year and returns a 4-digit year.


A value between 0-49 will return a 20xx year.
A value between 50-99 will return a 19xx year.

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the
month and ends on the seventh.

IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.


DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds. Use a value from 1 to 9 after FF to indicate


the number of digits in the fractional seconds. For example,
'FF4'.

AM, A.M., PM, Meridian indicator


or P.M.

AD or A.D AD indicator

BC or B.C. BC indicator

TZD Daylight savings information. For example, 'PST'

TZH Time zone hour.


TZM Time zone minute.

TZR Time zone region.

APPLIES TO
The TO_DATE 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 TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.

For example:

TO_DATE('2003/07/09', would return a date value of July


'yyyy/mm/dd') 9, 2003

TO_DATE('070903', 'MMDDYY') would return a date value of July


9, 2003

TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar


15, 2002

ORACLE/PLSQL: TO_DATE FUNCTION


Learn how to use the Oracle/PLSQL TO_DATE function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.

SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:

TO_DATE( string1, [ format_mask ], [ nls_language ] )


PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a date.

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

YYYY 4-digit year

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

RRRR Accepts a 2-digit year and returns a 4-digit year.


A value between 0-49 will return a 20xx year.
A value between 50-99 will return a 19xx year.

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.


RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the
month and ends on the seventh.

IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds. Use a value from 1 to 9 after FF to indicate


the number of digits in the fractional seconds. For example,
'FF4'.

AM, A.M., PM, Meridian indicator


or P.M.

AD or A.D AD indicator

BC or B.C. BC indicator

TZD Daylight savings information. For example, 'PST'

TZH Time zone hour.

TZM Time zone minute.

TZR Time zone region.

APPLIES TO
The TO_DATE 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 TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.

For example:

TO_DATE('2003/07/09', would return a date value of July


'yyyy/mm/dd') 9, 2003

TO_DATE('070903', 'MMDDYY') would return a date value of July


9, 2003

TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar


15, 2002
ORACLE/PLSQL: TRIM FUNCTION
Learn how to use the Oracle/PLSQL TRIM function with syntax and examples.

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:

TRIM( [ [ LEADING | TRAILING | BOTH ] trim_character FROM ] string1 )

PARAMETERS OR ARGUMENTS

LEADING - remove trim_character from the front of string1.

TRAILING - remove trim_character from the end of string1.

BOTH - remove trim_character from the front and end of string1.

trim_character is the character that will be removed from string1. If this parameter is omitted,
the TRIM function will remove space characters from string1.

string1 is the string to trim.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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'

TRIM(' ' FROM ' tech ') would return 'tech'

TRIM(LEADING '0' FROM '000123') would return '123'

TRIM(TRAILING '1' FROM 'Tech1') would return 'Tech'

TRIM(BOTH '1' FROM '123Tech111') would return '23Tech'

ORACLE/PLSQL: UPPER FUNCTION


Learn how to use the Oracle/PLSQL UPPER function with syntax and examples.

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

string1 is the string to convert to uppercase.

NOTE
See also the LOWER function.

APPLIES TO
The UPPER 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 UPPER function examples and explore how to use the UPPER
function in Oracle/PLSQL.

For example:

UPPER('Tech on the Net'); would return 'TECH ON THE NET'

UPPER('george burns 123 '); would return 'GEORGE BURNS 123 '

FREQUENTLY ASKED QUESTIONS


Question: How do you incorporate the Oracle UPPER function with the LIKE condition?
I'm trying to query against a free text field for all records containing the word "test". The
problem is that it can be entered in the following ways: TEST, Test, or test.

Answer: To answer this question, let's look at an example.

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:

select * from suppliers

where UPPER(supplier_name) like ('TEST%');

OR

select * from suppliers

where UPPER(supplier_name) like UPPER('test%')

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.

ORACLE/PLSQL: VSIZE FUNCTION


Learn how to use the Oracle/PLSQL VSIZE function with syntax and examples.

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

expression is the string to evaluate.

APPLIES TO
The VSIZE 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 VSIZE function examples and explore how to use the VSIZE
function in Oracle/PLSQL.

For example:

VSIZE('Tech on the net') would return 15

VSIZE('Tech on the net ') would return 16

VSIZE(null) would return <null>

VSIZE('') would return <null>

VSIZE(' ') would return 1

CONVERSION FUNCTIONS

ORACLE/PLSQL: BIN_TO_NUM FUNCTION


Learn how to use the Oracle/PLSQL BIN_TO_NUM function with syntax and examples.

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:

BIN_TO_NUM( expr1, expr2, ... expr_n)

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

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:

BIN_TO_NUM(1) would return 1

BIN_TO_NUM(1,0) would return 2

BIN_TO_NUM(1,1) would return 3

BIN_TO_NUM(1,1,1,0) would return 14

BIN_TO_NUM(1,1,1,1) would return 15

ORACLE/PLSQL: CAST FUNCTION


Learn how to use the Oracle/PLSQL CAST function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL CAST function converts one datatype to another.

SYNTAX
The syntax for the Oracle/PLSQL CAST function is:

CAST ( { expr | ( subquery ) | MULTISET ( subquery ) } AS type_name )

NOTE
The following casts are allowed:

TO FROM

char, number datetime raw rowid, nchar,


varchar2 / urowid nvarchar2
interval

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i


EXAMPLE
Let's look at some Oracle CAST function examples and explore how to use the CAST
function in Oracle/PLSQL.

For example:

select CAST( '22-Aug-2003' AS varchar2(30) )

from dual;

This would convert the date (ie: 22-Aug-2003) into a varchar2(30) value.

ORACLE/PLSQL: CHARTOROWID FUNCTION


Learn how to use the Oracle/PLSQL CHARTOROWID function with syntax and examples.

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

value1 is the value to convert to a rowid.

The format of the rowid is:

BBBBBBB.RRRR.FFFFF

where:

BBBBBBB is the block in the database file;


RRRR is the row in the block;
FFFFF is the database file.

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:

select * from suppliers

where rowid = CHARTOROWID('AAABoqAADAAAAwPAAA');

This would return a unique row from the suppliers table.

ORACLE/PLSQL: FROM_TZ FUNCTION


Learn how to use the Oracle/PLSQL FROM_TZ function with syntax and examples.

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:

FROM_TZ( timestamp_value, time_zone_value )

PARAMETERS OR ARGUMENTS

timestamp_value is the value to convert to a TIMESTAMP WITH TIME ZONE value.

time_zone_value is the time zone used to convert timestamp_value to a TIMESTAMP WITH


TIME ZONE value.

APPLIES TO
The FROM_TZ 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 FROM_TZ function examples and explore how to use the
FROM_TZ function in Oracle/PLSQL.

For example:

If you executed the following SQL statement:

select FROM_TZ(TIMESTAMP '2005-09-11 01:50:42', '5:00')

from dual;

You would now get the following result:

11-Sep-05 01.50.42.000000 AM +05:00

If you executed the same SQL statement, but set the time zone to '-7:00' as follows:

select FROM_TZ(TIMESTAMP '2005-09-11 01:50:42', '-7:00')

from dual;

You would now get the following result:

11-Sep-05 01.50.42.000000 AM -07:00

ORACLE/PLSQL: HEXTORAW FUNCTION


Learn how to use the Oracle/PLSQL HEXTORAW function with syntax and examples.

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

char is the hexademical value to convert to a raw value.

APPLIES TO
The HEXTORAW 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 HEXTORAW function examples and explore how to use the
HEXTORAW function in Oracle/PLSQL.

For example:

HEXTORAW('45D') would return '045D' as a raw value

HEXTORAW('7E') would return '7E' as a raw value

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:

NUMTODSINTERVAL( number, expression )

PARAMETERS OR ARGUMENTS

number is the number to convert to an interval.

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:

NUMTODSINTERVAL(150, 'DAY') would return '+000000150'

NUMTODSINTERVAL(1500, 'HOUR') would return '+000000062'

NUMTODSINTERVAL(15000, 'MINUTE') would return '+000000010'

NUMTODSINTERVAL(150000, 'SECOND') would return '+000000001'

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:

NUMTOYMINTERVAL( number, expression )

PARAMETERS OR ARGUMENTS

number is the number to convert to an interval.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle NUMTOYMINTERVAL function examples and explore how to use
the NUMTOYMINTERVAL function in Oracle/PLSQL.

For example:

NUMTOYMINTERVAL(100000000, 'MONTH') would return '+0083'

NUMTOYMINTERVAL(100000, 'YEAR') would return '+0001'

ORACLE/PLSQL: RAWTOHEX FUNCTION


Learn how to use the Oracle/PLSQL RAWTOHEX function with syntax and examples.

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

raw is the raw value to convert to a hexademical value.

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.

For example, if you ran the following:


DECLARE

a varchar2(8);

BEGIN

a := rawtohex('AB');

dbms_output.put_line(a);

SELECT RAWTOHEX('AB') INTO a FROM dual;

dbms_output.put_line(a);

END;

The following would be output as the result:

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle RAWTOHEX function examples and explore how to use the
RAWTOHEX function in Oracle/PLSQL.

For example:

RAWTOHEX('AB') would return '4142' if run as a SQL function and 'AB' if


run as a PLSQL function

RAWTOHEX('7E') would return '3745' if run as a SQL function and '7E' as


a PLSQL function

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:

TO_CHAR( value, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.

nls_language is optional. This is the nls language used to convert value to a string.

APPLIES TO
The TO_CHAR 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 TO_CHAR function examples and explore how to use the
TO_CHAR function in Oracle/PLSQL.
WITH NUMBERS

For example:

The following are number examples for the TO_CHAR function.

TO_CHAR(1210.73, '9999.9') would return ' 1210.7'

TO_CHAR(-1210.73, '9999.9') would return '-1210.7'

TO_CHAR(1210.73, '9,999.99') would return ' 1,210.73'

TO_CHAR(1210.73, '$9,999.00') would return ' $1,210.73'

TO_CHAR(21, '000099') would return ' 000021'

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

YEAR Year, spelled out

YYYY 4-digit year

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).


MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the year
and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the month
and ends on the seventh.

IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).
SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds.

The following are date examples for the TO_CHAR function.

TO_CHAR(sysdate, 'yyyy/mm/dd') would return '2003/07/09'

TO_CHAR(sysdate, 'Month DD, would return 'July 09, 2003'


YYYY')

TO_CHAR(sysdate, 'FMMonth DD, would return 'July 9, 2003'


YYYY')

TO_CHAR(sysdate, 'MON DDth, would return 'JUL 09TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMON DDth, would return 'JUL 9TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMon ddth, would return 'Jul 9th, 2003'


YYYY')

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.

TO_CHAR(sysdate, 'FMMonth DD, would return 'July 9, 2003'


YYYY')

TO_CHAR(sysdate, 'FMMON DDth, would return 'JUL 9TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMon ddth, would return 'Jul 9th, 2003'


YYYY')
The zeros have been suppressed so that the day component shows as "9" as opposed to
"09".

FREQUENTLY ASKED QUESTIONS


Question: Why doesn't this sort the days of the week in order?

SELECT ename, hiredate, TO_CHAR((hiredate),'fmDay') "Day"

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:

SELECT ename, hiredate, TO_CHAR((hiredate),'fmD') "Day"

FROM emp

ORDER BY "Day";

ORACLE/PLSQL: TO_CLOB FUNCTION


Learn how to use the Oracle/PLSQL TO_CLOB function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

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.

ORACLE/PLSQL: TO_DATE FUNCTION


Learn how to use the Oracle/PLSQL TO_DATE function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.

SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:

TO_DATE( string1, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a date.

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

YYYY 4-digit year

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

RRRR Accepts a 2-digit year and returns a 4-digit year.


A value between 0-49 will return a 20xx year.
A value between 50-99 will return a 19xx year.

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the
month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds. Use a value from 1 to 9 after FF to indicate


the number of digits in the fractional seconds. For example,
'FF4'.

AM, A.M., PM, Meridian indicator


or P.M.

AD or A.D AD indicator
BC or B.C. BC indicator

TZD Daylight savings information. For example, 'PST'

TZH Time zone hour.

TZM Time zone minute.

TZR Time zone region.

APPLIES TO
The TO_DATE 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 TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.

For example:

TO_DATE('2003/07/09', would return a date value of July


'yyyy/mm/dd') 9, 2003

TO_DATE('070903', 'MMDDYY') would return a date value of July


9, 2003

TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar


15, 2002

ORACLE/PLSQL: TO_DSINTERVAL FUNCTION


Learn how to use the Oracle/PLSQL TO_DSINTERVAL function with syntax and examples.

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:

TO_DSINTERVAL( character [ nls_parameter ] )

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.

nls_parameter is a decimal character and group separator value.

APPLIES TO
The TO_DSINTERVAL 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_DSINTERVAL function examples and explore how to use the
TO_DSINTERVAL function in Oracle/PLSQL.

For example:

TO_DSINTERVAL('150 08:30:00') would return '+000000150'

TO_DSINTERVAL('80 12:30:00') would return '+000000080'

TO_DSINTERVAL('95 18:30:00') would return '+000000095'

ORACLE/PLSQL: TO_LOB FUNCTION


Learn how to use the Oracle/PLSQL TO_LOB function with syntax and examples.

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

long_column can be a LONG or LONG RAW value.

APPLIES TO
The TO_LOB 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 TO_LOB function examples and explore how to use the TO_LOB
function in Oracle/PLSQL.

For example:

insert into companies (lob_column)

select TO_LOB(long_column)

from suppliers;

This example would convert the value in the field called long_column to a LOB value.

ORACLE/PLSQL: TO_MULTI_BYTE FUNCTION


Learn how to use the Oracle/PLSQL TO_MULTI_BYTE function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

select TO_MULTI_BYTE('Tech on the net')

from dual;

The SQL statement above would return a multibyte character value

ORACLE/PLSQL: TO_NCLOB FUNCTION


Learn how to use the Oracle/PLSQL TO_NCLOB function with syntax and examples.

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

expression can be a char, varchar2, nchar, nvarchar2, clob or nclob value.

APPLIES TO
The TO_NCLOB 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_NCLOB function examples and explore how to use the
TO_NCLOB function in 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.

ORACLE/PLSQL: TO_NUMBER FUNCTION


Learn how to use the Oracle/PLSQL TO_NUMBER function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_NUMBER function converts a string to a number.

SYNTAX
The syntax for the Oracle/PLSQL TO_NUMBER function is:

TO_NUMBER( string1, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a number.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

TO_NUMBER('1210.73', would return the number


'9999.99') 1210.73

TO_NUMBER('546', '999') would return the number 546

TO_NUMBER('23', '99') would return the number 23

Since the format_mask and nls_language parameters are optional, you can simply convert a
text string to a numeric value as follows:

TO_NUMBER('1210.73') would return the number 1210.73

ORACLE/PLSQL: TO_SINGLE_BYTE FUNCTION


Learn how to use the Oracle/PLSQL TO_SINGLE_BYTE function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

select TO_SINGLE_BYTE('Tech on the net')

from dual;

The SQL statement above would return a single-byte character value.

ORACLE/PLSQL: TO_TIMESTAMP FUNCTION


Learn how to use the Oracle/PLSQL TO_TIMESTAMP function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_TIMESTAMP function converts a string to a timestamp.

SYNTAX
The syntax for the Oracle/PLSQL TO_TIMESTAMP function is:

TO_TIMESTAMP( string1, [ format_mask ] [ 'nlsparam' ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a timestamp.

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

YYYY 4-digit year

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.


MONTH Name of month, padded with blanks to length of 9 characters.

DD Day of month (1-31).

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

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:

TO_TIMESTAMP('2003/12/13 10:13:18', 'YYYY/MM/DD HH:MI:SS')

would return '13-DEC-03 10.13.18.000000000 AM' as a timestamp value.

TO_TIMESTAMP('2003/DEC/13 10:13:18', 'YYYY/MON/DD HH:MI:SS')

would also return '13-DEC-03 10.13.18.000000000 AM' as a timestamp value.

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:

TO_TIMESTAMP_TZ( string1 , [ format_mask ] [ 'nlsparam' ] )

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

YYYY 4-digit year

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

DD Day of month (1-31).

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).


MI Minute (0-59).

SS Second (0-59).

TZM Time zone minute.

TZH Time zone hour.

APPLIES TO
The TO_TIMESTAMP_TZ 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_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 return '13-DEC-03 10.13.18.000000000 AM -08:00' as a timestamp with time zone


value.

TO_TIMESTAMP_TZ('2003/DEC/13 10:13:18 -8:00', 'YYYY/MON/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.

ORACLE/PLSQL: TO_YMINTERVAL FUNCTION


Learn how to use the Oracle/PLSQL TO_YMINTERVAL function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

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:

TO_YMINTERVAL('03- would return 3 years 11 months as an


11') INTERVAL YEAR TO MONTH type

TO_YMINTERVAL('01- would return 1 year 5 months as an


05') INTERVAL YEAR TO MONTH type

TO_YMINTERVAL('00- would return 0 years 1 month as an


01') INTERVAL YEAR TO MONTH type

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.

select order_date, order_date + to_yminterval('01-05')

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:

BFILENAME( 'directory', 'filename' )

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.

filename is the name of the file on the file server.

APPLIES TO
The BFILENAME 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 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.

CREATE DIRECTORY exampleDir AS '/example/totn';

Then we can use the exampleDir directory object in the BFILENAME function as follows:

SELECT BFILENAME('exampleDir', 'totn_logo.png')


FROM dual;

ORACLE/PLSQL: CARDINALITY FUNCTION


Learn how to use the Oracle/PLSQL CARDINALITY function with syntax and examples.

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:

CARDINALITY( nested table column )

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:

Oracle 12c, Oracle 11g, Oracle 10g

EXAMPLE
Let's look at some Oracle CARDINALITY function examples and explore how to use the
CARDINALITY function in Oracle/PLSQL.

For example:

select supplier_id, CARDINALITY(location)

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 ]

WHEN condition_1 THEN result_1

WHEN condition_2 THEN result_2

...

WHEN condition_n THEN result_n

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

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

WHEN 'SYS' THEN 'The owner is SYS'

WHEN 'SYSTEM' THEN 'The owner is SYSTEM'

ELSE 'The owner is another value'

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

WHEN owner='SYS' THEN 'The owner is SYS'

WHEN owner='SYSTEM' THEN 'The owner is SYSTEM'


ELSE 'The owner is another value'

END

FROM all_tables;

The above two CASE statements are equivalent to the following IF-THEN-ELSE statement:

IF owner = 'SYS' THEN

result := 'The owner is SYS';

ELSIF owner = 'SYSTEM' THEN

result := 'The owner is SYSTEM'';

ELSE

result := 'The owner is another value';

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.

Your SQL statement would look as follows:

SELECT table_name,

CASE owner

WHEN 'SYS' THEN 'The owner is SYS'

WHEN 'SYSTEM' THEN 'The owner is SYSTEM'

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

WHEN a < b THEN 'hello'

WHEN d < e THEN 'goodbye'

END

FROM suppliers;

FREQUENTLY ASKED QUESTIONS


Question: Can you create a CASE statement that evaluates two different fields? I want to
return a value based on the combinations in two different fields.

Answer: Yes, below is an example of a case statement that evaluates two different fields.

SELECT supplier_id,

CASE

WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North


office'

WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South


office'

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.

ORACLE/PLSQL: COALESCE FUNCTION


Learn how to use the Oracle/PLSQL COALESCE function with syntax and examples.

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:

COALESCE( expr1, expr2, ... expr_n )

PARAMETERS OR ARGUMENTS

expr1 to expr_n are the expressions to test for non-null values.

APPLIES TO
The COALESCE function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE
The COALESCE function can be used in Oracle/PLSQL.

You could use the coalesce function in a SQL statement as follows:

SELECT COALESCE( address1, address2, address3 ) result

FROM suppliers;

The above COALESCE function is equivalent to the following IF-THEN-ELSE statement:

IF address1 is not null THEN

result := address1;

ELSIF address2 is not null THEN

result := address2;
ELSIF address3 is not null THEN

result := address3;

ELSE

result := null;

END IF;

The COALESCE function will compare each value, one by one.

ORACLE/PLSQL: DECODE FUNCTION


Learn how to use the Oracle/PLSQL DECODE function with syntax and examples.

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:

DECODE( expression , search , result [, search , result]... [, default] )

PARAMETERS OR ARGUMENTS

expression is the value to compare.

search is the value that is compared against expression.

result is the value returned, if expression is equal to search.

default is optional. If no matches are found, the DECODE 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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i


EXAMPLE
The DECODE function can be used in Oracle/PLSQL.

You could use the DECODE function in a SQL statement as follows:

SELECT supplier_name,

DECODE(supplier_id, 10000, 'IBM',

10001, 'Microsoft',

10002, 'Hewlett Packard',

'Gateway') result

FROM suppliers;

The above DECODE statement is equivalent to the following IF-THEN-ELSE statement:

IF supplier_id = 10000 THEN

result := 'IBM';

ELSIF supplier_id = 10001 THEN

result := 'Microsoft';

ELSIF supplier_id = 10002 THEN

result := 'Hewlett Packard';

ELSE

result := 'Gateway';

END IF;
The DECODE function will compare each supplier_id value, one by one.

FREQUENTLY ASKED QUESTIONS


Question: One of our viewers wanted to know how to use the DECODE function to compare
two dates (ie: date1 and date2), where if date1 > date2, the DECODE function should return
date2. Otherwise, the DECODE function should return date1.

Answer: To accomplish this, use the DECODE function as follows:

DECODE((date1 - date2) - ABS(date1 - date2), 0, date2, date1)

The formula below would equal 0, if date1 is greater than date2:

(date1 - date2) - ABS(date1 - date2)

Helpful Tip #1: One of our viewers suggested combining the SIGN function with the
DECODE function as follows:

The date example above could be modified as follows:

DECODE(SIGN(date1-date2), 1, date2, date1)

The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses

DECODE(SIGN(actual-target), -1, 'NO Bonus for you', 0,'Just made it', 1,


'Congrats, you are a winner')

Helpful Tip #2: One of our viewers suggested using the LEAST function (instead of the
DECODE function) as follows:

The date example above could be modified 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,

DECODE(TRUNC ((supplier_id - 1) / 10), 0, 'category 1',

1, 'category 2',

2, 'category 3',

'unknown') result

FROM suppliers;

In this example, based on the formula:

TRUNC ((supplier_id - 1) / 10

The formula will evaluate to 0, if the supplier_id is between 1 and 10.


The formula will evaluate to 1, if the supplier_id is between 11 and 20.
The formula will evaluate to 2, if the supplier_id is between 21 and 30.

and so on...

Question: I need to write a DECODE statement that will return the following:

If yrs_of_service < 1 then return 0.04

If yrs_of_service >= 1 and < 5 then return 0.04

If yrs_of_service > 5 then return 0.06

How can I do this?

Answer: You will need to create a formula that will evaluate to a single number for each one
of your ranges.

For example:

SELECT emp_name,

DECODE(TRUNC (( yrs_of_service + 3) / 4), 0, 0.04,

1, 0.04,

0.06) as perc_value
FROM employees;

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.

ORACLE/PLSQL: EMPTY_BLOB FUNCTION


Learn how to use the Oracle/PLSQL EMPTY_BLOB function with syntax and examples.

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

There are no parameters or arguments for the EMPTY_BLOB function.

APPLIES TO
The EMPTY_BLOB 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 EMPTY_BLOB function examples and explore how to use the
EMPTY_BLOB function in Oracle/PLSQL.

For example:

UPDATE products

SET product_photo = EMPTY_BLOB();


This EMPTY_BLOB example would initialize the product_photo column to EMPTY. This is
different than setting the column to a null LOB or a LOB with a string length of 0.

ORACLE/PLSQL: EMPTY_CLOB FUNCTION


Learn how to use the Oracle/PLSQL EMPTY_CLOB function with syntax and examples.

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

There are no parameters or arguments for the EMPTY_CLOB function.

APPLIES TO
The EMPTY_CLOB 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 EMPTY_CLOB function examples and explore how to use the
EMPTY_CLOB function in Oracle/PLSQL.

For example:

UPDATE customers

SET customer_photo = EMPTY_CLOB();

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.

ORACLE/PLSQL: GROUP_ID FUNCTION


Learn how to use the Oracle/PLSQL GROUP_ID function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL GROUP_ID function assigns a number to each group resulting from
a GROUP BY clause. The GROUP_ID function is most commonly used to identify duplicated
groups in your query results.

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:

SELECT column1, column2, ... column_n, GROUP_ID()

FROM tables

WHERE conditions

GROUP BY column1, column2, ... column_n;

PARAMETERS OR ARGUMENTS

There are no parameters or arguments for the GROUP_ID function.

APPLIES TO
The GROUP_ID 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 GROUP_ID function examples and explore how to use the
GROUP_ID function in Oracle/PLSQL.

For example:

SELECT SUM(salary), department, bonus, GROUP_ID()

FROM employees

WHERE bonus > 100

GROUP BY department,
ROLLUP (department, bonus);

You could use the HAVING clause to eliminated duplicated groups as follows:

SELECT SUM(salary), department, bonus, GROUP_ID()

FROM employees

WHERE bonus > 100

GROUP BY department,

ROLLUP (department, bonus)

HAVING GROUP_ID() < 1;

ORACLE/PLSQL: LAG FUNCTION


Learn how to use the Oracle/PLSQL LAG function with syntax and examples.

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:

LAG ( expression [, offset [, default] ] )

over ( [ query_partition_clause ] order_by_clause )

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

ORDER_DATE PRODUCT_ID QTY

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

And we ran the following SQL statement:

select product_id, order_date,

LAG (order_date,1) over (ORDER BY order_date) AS prev_order_date

from orders;

It would return the following result:

PRODUCT_ID ORDER_DATE PREV_ORDER_DATE

1000 25/09/2007 <NULL>


2000 26/09/2007 25/09/2007

1000 27/09/2007 26/09/2007

2000 28/09/2007 27/09/2007

2000 29/09/2007 28/09/2007

1000 30/09/2007 29/09/2007

Since we used an offset of 1, the query returns the previous order_date.

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:

select product_id, order_date,

LAG (order_date,1) over (ORDER BY order_date) AS prev_order_date

from orders

where product_id = 2000;

It would return the following result:

PRODUCT_ID ORDER_DATE PREV_ORDER_DATE

2000 26/09/2007 <NULL>

2000 28/09/2007 26/09/2007

2000 29/09/2007 28/09/2007

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:

LEAD ( expression [, offset [, default] ] )

over ( [ query_partition_clause ] order_by_clause )

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

ORDER_DATE PRODUCT_ID QTY

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

And we ran the following SQL statement:

select product_id, order_date,

LEAD (order_date,1) over (ORDER BY order_date) AS next_order_date

from orders;

It would return the following result:

PRODUCT_ID ORDER_DATE NEXT_ORDER_DATE

1000 25/09/2007 26/09/2007

2000 26/09/2007 27/09/2007

1000 27/09/2007 28/09/2007

2000 28/09/2007 29/09/2007

2000 29/09/2007 30/09/2007

1000 30/09/2007 <NULL>

Since we used an offset of 1, the query returns the next order_date.


If we had used an offset of 2 instead, it would have returned the order_date from 2 orders
later. If we had used an offset of 3, it would have returned the order_date from 3 orders
later....and so on.

If we wanted only the orders for a given product_id, we could run the following SQL
statement:

select product_id, order_date,

LEAD (order_date,1) over (ORDER BY order_date) AS next_order_date

from orders

where product_id = 2000;

It would return the following result:

PRODUCT_ID ORDER_DATE NEXT_ORDER_DATE

2000 26/09/2007 28/09/2007

2000 28/09/2007 29/09/2007

2000 29/09/2007 <NULL>

In this example, it returned the next order_date for product_id = 2000 and ignored all other
orders.

ORACLE/PLSQL: LISTAGG FUNCTION


Learn how to use the Oracle/PLSQL LISTAGG function with syntax and examples.

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:

LISTAGG (measure_column [, 'delimiter'])


WITHIN GROUP (order_by_clause) [OVER query_partition_clause]

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:

Oracle 12c, Oracle 11g Release 2

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.

If you had a products table with the following data:

product_id product_name

1001 Bananas

1002 Apples

1003 Pears

1004 Oranges

And then you executed a SELECT statement using the LISTAGG function:

SELECT LISTAGG(product_name, ', ') WITHIN GROUP (ORDER BY product_name)


"Product_Listing"
FROM products;

You would get the following results:

Product_Listing

Apples, Bananas, Oranges, Pears

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:

SELECT LISTAGG(product_name, ', ') WITHIN GROUP (ORDER BY product_name


DESC) "Product_Listing"

FROM products;

This would give the following results:

Product_Listing

Pears, Oranges, Bananas, Apples

You could change the delimiter from a comma to a semi-colon as follows:

SELECT LISTAGG(product_name, '; ') WITHIN GROUP (ORDER BY product_name


DESC) "Product_Listing"

FROM products;

This would change your results as follows:

Product_Listing

Pears; Oranges; Bananas; Apples

ORACLE/PLSQL: LNNVL FUNCTION


Learn how to use the Oracle/PLSQL LNNVL function with syntax and examples.

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:

Condition Evaluates To LNNVL Return Value

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:

Condition Condition Evaluates To LNNVL Return Value

qty = reorder_level UNKNOWN TRUE

qty IS NULL FALSE TRUE

reorder_level IS NULL TRUE FALSE

qty = 20 TRUE FALSE


reorder_level = 20 UNKNOWN TRUE

APPLIES TO
The LNNVL function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g

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:

PRODUCT_ID QTY REORDER_LEVEL

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:

select * from products

where qty < reorder_level;

This would return the following result:


PRODUCT_ID QTY REORDER_LEVEL

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:

select * from products

where LNNVL(qty >= reorder_level);

This would return the following result:

PRODUCT_ID QTY REORDER_LEVEL

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.

ORACLE/PLSQL: NANVL FUNCTION


Learn how to use the Oracle/PLSQL NANVL function with syntax and examples.

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

value is the BINARY_FLOAT or BINARY_NUMBER to test for a Nan (Not a number).

replace_with is the value returned if value is Nan (not a number).

APPLIES TO
The NANVL function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g

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.

Another example of the NANVL function in Oracle/PLSQL is:

select NANVL(binary1, NULL)

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.

ORACLE/PLSQL: NULLIF FUNCTION


Learn how to use the Oracle/PLSQL NULLIF function with syntax and examples.

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:

NULLIF( expr1, expr2 )

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE
Let's look at some Oracle NULLIF function examples and explore how to use the NULLIF
function in Oracle/PLSQL.

For example:

NULLIF(12, 12) would return NULL

NULLIF(12, 13) would return 12

NULLIF('apples', 'apples') would return NULL

NULLIF('apples', would return 'apples'


'oranges')

NULLIF(NULL, 12) would return an ORA-00932 error


because
expr1 can not be the literal NULL

ORACLE/PLSQL: NVL FUNCTION


Learn how to use the Oracle/PLSQL NVL function with syntax and examples.

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:

NVL( string1, replace_with )

PARAMETERS OR ARGUMENTS

string1 is the string to test for a null value.

replace_with is the value returned if string1 is null.

APPLIES TO
The NVL 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 NVL function examples and explore how to use the NVL function in
Oracle/PLSQL.

For example:

SELECT NVL(supplier_city, 'n/a')

FROM suppliers;

The SQL statement above would return 'n/a' if the supplier_city field contained a null value.
Otherwise, it would return the supplier_city value.

Another example using the NVL function in Oracle/PLSQL is:

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.

A final example using the NVL function in Oracle/PLSQL is:

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.

FREQUENTLY ASKED QUESTIONS


Question: I tried to use the NVL function through VB to access Oracle DB.

To be precise,

SELECT NVL(Distinct (emp_name),'AAA'),................

FROM.................

I got an oracle error when I use distinct clause with NVL, but when I remove distinct it works
fine.

Answer: It is possible to the use the DISTINCT clause with the NVL function. However, the
DISTINCT must come before the use of the NVL function. For example:

SELECT distinct NVL(emp_name, 'AAA')

FROM employees;

Hope this helps!

Question: Is it possible to use the NVL function with more than one column with the same
function call? To be clear, if i need to apply this NVL function to more than one column like
this:

NVL(column1;column2 ...... , here is the default value for all )

Answer: You will need to make separate NVL function calls for each column. For example:

SELECT NVL(table_name, 'not found'), NVL(owner, 'not found')


FROM all_tables;

ORACLE/PLSQL: NVL2 FUNCTION


Learn how to use the Oracle/PLSQL NVL2 function with syntax and examples.

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:

NVL2( string1, value_if_NOT_null, value_if_null )

PARAMETERS OR ARGUMENTS

string1 is the string to test for a null value.

value_if_NOT_null is the value returned if string1 is not null.

value_if_null is the value returned if string1 is null.

APPLIES TO
The NVL2 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 NVL2 function examples and explore how to use the NVL2 function
in Oracle/PLSQL.

For example:

select NVL2(supplier_city, 'Completed', 'n/a')

from suppliers;

The SQL statement above would return 'n/a' if the supplier_city field contained a null value.
Otherwise, it would return the 'Completed'.
Another example using the NVL2 function in Oracle/PLSQL is:

select supplier_id,

NVL2(supplier_desc, supplier_name, supplier_name2)

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.

ORACLE/PLSQL: SYS_CONTEXT FUNCTION


Learn how to use the Oracle/PLSQL SYS_CONTEXT function with syntax and examples.

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:

SYS_CONTEXT( namespace, parameter, [ length ] )

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)

Parameter Explanation Orac Orac Orac


le 9i le le
10g 11g
ACTION Returns the position in the No Yes Yes
module

AUDITED_CURSORID Returns the cursor ID of Yes Yes Yes


the SQL that triggered the
audit

AUTHENTICATED_IDENTI Returns the identity used No Yes Yes


TY in authentication

AUTHENTICATION_DATA Authentication data Yes Yes Yes

AUTHENTICATION_METH Returns the method of No Yes Yes


OD authentication

AUTHENTICATION_TYPE Describes how the user Yes No No


was authenticated. Can be
one of the following
values: Database, OS,
Network, or Proxy

BG_JOB_ID If the session was Yes Yes Yes


established by an Oracle
background process, this
parameter will return the
Job ID. Otherwise, it will
return NULL.

CLIENT_IDENTIFIER Returns the client Yes Yes Yes


identifier (global context)

CLIENT_INFO User session information Yes Yes Yes

CURRENT_BIND Bind variables for fine- No Yes Yes


grained auditing

CURRENT_SCHEMA Returns the default Yes Yes Yes


schema used in the current
schema
CURRENT_SCHEMAID Returns the identifier of Yes Yes Yes
the default schema used in
the current schema

CURRENT_SQL Returns the SQL that Yes Yes Yes


triggered the audit event

CURRENT_SQL_LENGTH Returns the length of the No Yes Yes


current SQL statement
that triggered the audit
event

CURRENT_USER Name of the current user Yes No No

CURRENT_USERID Userid of the current user Yes No No

DB_DOMAIN Domain of the database Yes Yes Yes


from the DB_DOMAIN
initialization parameter

DB_NAME Name of the database Yes Yes Yes


from the DB_NAME
initialization parameter

DB_UNIQUE_NAME Name of the database No Yes Yes


from the
DB_UNIQUE_NAME
initialization parameter

ENTRYID Available auditing entry Yes Yes Yes


identifier

ENTERPRISE_IDENTITY Returns the user's No Yes Yes


enterprise-wide identity

EXTERNAL_NAME External of the database Yes No No


user
FG_JOB_ID If the session was Yes Yes Yes
established by a client
foreground process, this
parameter will return the
Job ID. Otherwise, it will
return NULL.

GLOBAL_CONTEXT_MEM The number used in the Yes Yes Yes


ORY System Global Area by
the globally accessed
context

GLOBAL_UID The global user ID from No No Yes


Oracle Internet Directory
for enterprise security
logins. Returns NULL for
all other logins.

HOST Name of the host machine Yes Yes Yes


from which the client has
connected

IDENTIFICATION_TYPE Returns the way the user's No Yes Yes


schema was created

INSTANCE The identifier number of Yes Yes Yes


the current instance

INSTANCE_NAME The name of the current No Yes Yes


instance

IP_ADDRESS IP address of the machine Yes Yes Yes


from which the client has
connected

ISDBA Returns TRUE if the user Yes Yes Yes


has DBA privileges.
Otherwise, it will return
FALSE.
LANG The ISO abbreviate for the Yes Yes Yes
language

LANGUAGE The language, territory, Yes Yes Yes


and character of the
session. In the following
format:
language_territory.charact
erset

MODULE Returns the appplication No Yes Yes


name set through
DBMS_APPLICATION_I
NFO package or OCI

NETWORK_PROTOCOL Network protocol used Yes Yes Yes

NLS_CALENDAR The calendar of the Yes Yes Yes


current session

NLS_CURRENCY The currency of the Yes Yes Yes


current session

NLS_DATE_FORMAT The date format for the Yes Yes Yes


current session

NLS_DATE_LANGUAGE The language used for Yes Yes Yes


dates

NLS_SORT BINARY or the linguistic Yes Yes Yes


sort basis

NLS_TERRITORY The territory of the current Yes Yes Yes


session

OS_USER The OS username for the Yes Yes Yes


user logged in
POLICY_INVOKER The invoker of row-level No Yes Yes
security policy functions

PROXY_ENTERPRISE_IDE The Oracle Internet No Yes Yes


NTITY Directory DN when the
proxy user is an enterprise
user

PROXY_GLOBAL_UID The global user ID from No Yes Yes


Oracle Internet Directory
for enterprise user security
proxy users. Returns
NULL for all other proxy
users.

PROXY_USER The name of the user who Yes Yes Yes


opened the current session
on behalf of
SESSION_USER

PROXY_USERID The identifier of the user Yes Yes Yes


who opened the current
session on behalf of
SESSION_USER

SERVER_HOST The host name of the No Yes Yes


machine where the
instance is running

SERVICE_NAME The name of the service No Yes Yes


that the session is
connected to

SESSION_USER The database user name of Yes Yes Yes


the user logged in

SESSION_USERID The database identifier of Yes Yes Yes


the user logged in
SESSIONID The identifier of the Yes Yes Yes
auditing session

SID Session number No Yes Yes

STATEMENTID The auditing statement No Yes Yes


identifier

TERMINAL The OS identifier of the Yes Yes Yes


current session

APPLIES TO
The SYS_CONTEXT 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 SYS_CONTEXT function examples and explore how to use the
SYS_CONTEXT function in Oracle/PLSQL.

For example:

SYS_CONTEXT('USERENV', would return 'RR-MM-


'NLS_DATE_FORMAT') DD'

SYS_CONTEXT('USERENV', 'NLS_SORT') would return 'BINARY'

ORACLE/PLSQL: UID FUNCTION


Learn how to use the Oracle/PLSQL UID function with syntax and examples.

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

There are no parameters or arguments for the UID function.

APPLIES TO
The UID 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 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.

ORACLE/PLSQL: USER FUNCTION


Learn how to use the Oracle/PLSQL USER function with syntax and examples.

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

There are no parameters or arguments for the USER function.


APPLIES TO
The USER 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 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.

ORACLE/PLSQL: USERENV FUNCTION


Learn how to use the Oracle/PLSQL USERENV function with syntax and examples.

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

CLIENT_INFO Returns user session information stored using the


DBMS_APPLICATION_INFO package

ENTRYID Available auditing entry identifier

INSTANCE The identifier number of the current instance

ISDBA Returns TRUE if the user has DBA privileges. Otherwise, it will return FALSE.

LANG The ISO abbreviation for the language

LANGUAGE The language, territory, and character of the session. In the following
format:
language_territory.characterset

SESSIONID The identifier of the auditing session

TERMINAL The OS identifier of the current session

APPLIES TO
The USERENV 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 USERENV function examples and explore how to use the
USERENV function in Oracle/PLSQL.

For example:

USERENV('ENTRYID') would return FALSE

USERENV('LANGUAGE') would return 'AMERICAN_AMERICA.WE8DEC'


NUMERIC / MATHEMATICAL FUNCTIONS

ORACLE/PLSQL: ABS FUNCTION


Learn how to use the Oracle/PLSQL ABS function with syntax and examples.

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

number is the number to convert to an absolute value.

APPLIES TO
The ABS 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 ABS function examples and explore how to use the ABS function in
Oracle/PLSQL.

For example:

ABS(-23) would return 23

ABS(-23.6) would return 23.6

ABS(-23.65) would return 23.65

ABS(23.65) would return 23.65


ABS(23.65 * -1) would return 23.65

ORACLE/PLSQL: ACOS FUNCTION


Learn how to use the Oracle/PLSQL ACOS function with syntax and examples.

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

number is the number used to calculate the arc cosine.

APPLIES TO
The ACOS 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 ACOS function examples and explore how to use the ACOS
function in Oracle/PLSQL.

For example:

ACOS(0.2) would return 1.36943840600457

ACOS(0.35) would return 1.21322522314939

ACOS(-0.15) would return 1.72136459957158


ORACLE/PLSQL: ASIN FUNCTION
Learn how to use the Oracle/PLSQL ASIN function with syntax and examples.

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

number is the number used to calculate the arc sine.

APPLIES TO
The ASIN 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 ASIN function examples and explore how to use the ASIN function
in Oracle/PLSQL.

For example:

ASIN(0.2) would return 0.201357920790331

ASIN(0.35) would return 0.35757110364551

ASIN(-0.15) would return -0.150568272776686

ORACLE/PLSQL: ATAN FUNCTION


Learn how to use the Oracle/PLSQL ATAN function with syntax and examples.

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

number is the number used to calculate the arc tangent.

APPLIES TO
The ATAN 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 ATAN function examples and explore how to use the ATAN
function in Oracle/PLSQL.

For example:

ATAN(0.2) would return 0.197395559849881

ATAN(0.35) would return 0.336674819386727

ATAN(-0.15) would return -0.148889947609497

ORACLE/PLSQL: ATAN2 FUNCTION


Learn how to use the Oracle/PLSQL ATAN2 function with syntax and examples.

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

n and m are the numbers used to calculate the arc tangent.

APPLIES TO
The ATAN2 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 ATAN2 function examples and explore how to use the ATAN2
function in Oracle/PLSQL.

For example:

ATAN2(0.2, 0.3) would return 0.588002603547568

ATAN2(0.35, -0.15) would return 1.97568811307998

ATAN2(-0.3, -0.4) would return -2.49809154479651

ORACLE/PLSQL: AVG FUNCTION


Learn how to use the Oracle/PLSQL AVG function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL AVG function returns the average value of an expression.

SYNTAX
The syntax for the Oracle/PLSQL AVG function is:

SELECT AVG( expression )

FROM tables

WHERE conditions;
PARAMETERS OR ARGUMENTS

expression can be a numeric field or formula.

APPLIES TO
The AVG function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE - WITH SINGLE FIELD


Let's look at some Oracle AVG function examples and explore how to use the AVG function
in Oracle/PLSQL.

For example, you might wish to know how the average salary of all employees whose salary
is above $25,000 / year.

SELECT AVG(salary) AS "Avg Salary"

FROM employees

WHERE salary > 25000;

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.

EXAMPLE - USING DISTINCT


You can use the DISTINCT clause within the AVG function. For example, the SQL statement
below returns the average salary of unique salary values where the salary is above $25,000 /
year.

SELECT AVG(DISTINCT salary) AS "Avg Salary"

FROM employees

WHERE salary > 25000;

If there were two salaries of $30,000/year, only one of these values would be used in the
AVG function.

EXAMPLE - USING FORMULA


The expression contained within the AVG function does not need to be a single field. You
could also use a formula. For example, you might want the average commission.
SELECT AVG(sales * 0.10) AS "Average Commission"

FROM orders;

EXAMPLE - USING GROUP BY


You could also use the AVG function to return the name of the department and the average
sales (in the associated department). For example,

SELECT department, AVG(sales) AS "Avg sales"

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.

ORACLE/PLSQL: BIN_TO_NUM FUNCTION


Learn how to use the Oracle/PLSQL BIN_TO_NUM function with syntax and examples.

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:

BIN_TO_NUM( expr1, expr2, ... expr_n)

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

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:

BIN_TO_NUM(1) would return 1

BIN_TO_NUM(1,0) would return 2

BIN_TO_NUM(1,1) would return 3

BIN_TO_NUM(1,1,1,0) would return 14

BIN_TO_NUM(1,1,1,1) would return 15

ORACLE/PLSQL: BITAND FUNCTION


Learn how to use the Oracle/PLSQL BITAND function with syntax and examples.

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:

bitand( expr1, expr2 )

PARAMETERS OR ARGUMENTS

expr1 and expr2 must resolve to non-negative integers.

APPLIES TO
The BITAND 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 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

ORACLE/PLSQL: CEIL FUNCTION


Learn how to use the Oracle/PLSQL CEIL function with syntax and
examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle CEIL function examples and explore how to use
the CEIL function in Oracle/PLSQL.

For example:

CEIL(32.65) would return 33

CEIL(32.1) would return 33

CEIL(32) would return 32

CEIL(-32.65) would return -32

CEIL(-32) would return -32

ORACLE/PLSQL: CORR FUNCTION


Learn how to use the Oracle/PLSQL CORR function with syntax and
examples.

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:

CORR( n ,m ) [ over (analytic_clause) ]


PARAMETERS OR ARGUMENTS

n and m are the numbers to use to calculate the cofficient of correlation.

NOTE
The CORR function requires at least two rows.

APPLIES TO
The CORR 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 CORR function examples and explore how to
use the CORR function in Oracle/PLSQL.

For example:

select CORR(quantity, commission)

from sales;

Here is an example using the GROUP BY clause:

select max_entents, CORR(max_trans, initial_extent)

from data

group by max_extents;

ORACLE/PLSQL: COS FUNCTION


Learn how to use the Oracle/PLSQL COS function with syntax and
examples.

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

number is the number used to calculate the cosine.

APPLIES TO
The COS 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 COS function examples and explore how to use
the COS function in Oracle/PLSQL.

For example:

COS(0.2) would return 0.980066577841242

COS(0.35) would return 0.939372712847379

COS(-3.15) would return -0.999964658471342

COS(200) would return 0.487187675007006

ORACLE/PLSQL: COSH FUNCTION


Learn how to use the Oracle/PLSQL COSH function with syntax and
examples.

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

number is the number used to calculate the hyperbolic cosine.

APPLIES TO
The COSH 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 COSH function examples and explore how to
use the COSH function in Oracle/PLSQL.

For example:

COSH(0.2) would return 1.02006675561908

COSH(0.35) would return 1.06187781915599

COSH(-3.15) would return 11.6894583539049

COSH(200) would return 3.61298688406287E86

COSH(0) 1

ORACLE/PLSQL: COUNT FUNCTION


Learn how to use the Oracle/PLSQL COUNT function with syntax and
examples.

DESCRIPTION
The Oracle/PLSQL COUNT function returns the count of an expression.
SYNTAX
The syntax for the Oracle/PLSQL COUNT function is:

SELECT COUNT( expression )

FROM tables

WHERE conditions;

PARAMETERS OR ARGUMENTS

expression can be a field or formula.

ONLY INCLUDES NOT NULL VALUES


Not everyone realizes this, but the COUNT function will only include the
records in the count where the value of expression in COUNT(expression)
is NOT NULL. When expression contains a NULL value, it is not included
in the COUNT calculations.

Let's look at a COUNT function example that demonstrates how NULL


values are evaluated by the COUNT function.

For example, if you have the following table called suppliers:

supplier_id supplier_name state

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE - WITH SINGLE FIELD


Let's look at some Oracle COUNT function examples and explore how to
use the COUNT function in Oracle/PLSQL.

For example, you might wish to know how many employees have a salary
above $75,000 / year.

SELECT COUNT(*) AS "Number of employees"

FROM employees

WHERE salary > 75000;

In this COUNT function example, we've aliased the COUNT(*) expression


as "Number of employees". As a result, "Number of employees" will
display as the field name when the result set is returned.

EXAMPLE - USING DISTINCT


You can use the DISTINCT clause within the COUNT function. For
example, the SQL statement below returns the number of unique
departments where at least one employee makes over $55,000 / year.

SELECT COUNT(DISTINCT department) AS "Unique departments"


FROM employees

WHERE salary > 55000;

Again, the COUNT(DISTINCT department) field is aliased as "Unique


departments". This is the field name that will display in the result set.

EXAMPLE - USING GROUP BY


In some cases, you will be required to use the GROUP BY clause with the
COUNT function.

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'.

SELECT department, COUNT(*) AS "Number of employees"

FROM employees

WHERE state = 'CA'

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

expression1 is a numeric expression.

expression2 is a numeric expression.

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:

Oracle 12c, Oracle 11g, Oracle 10g,


Oracle 9i, Oracle 8i

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:

select owner, COVAR_POP(avg_row_len, avg_space)

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:

COVAR_SAMP( expression1, expression2 )

PARAMETERS OR ARGUMENTS

expression1 is a numeric expression.

expression2 is a numeric expression.

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:

Oracle 12c, Oracle 11g, Oracle 10g,


Oracle 9i, Oracle 8i

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:

select owner, COVAR_SAMP(avg_row_len, avg_space)

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.

The CUME_DIST function can be used two ways - as an Aggregate


function or as an Analytic function.

CUME_DIST FUNCTION SYNTAX #1 - USED AS


AN AGGREGATE FUNCTION
As an Aggregate function, the CUME_DIST function returns the relative
position of a row within a group of rows.

The syntax for the CUME_DIST function when used as an Aggregate


function is:

CUME_DIST( expression1, ... expression_n ) WITHIN GROUP (


ORDER BY expression1, ... expression_n )

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 CUME_DIST function can be used in the following versions of
Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g,


Oracle 9i

EXAMPLE (AS AN AGGREGATE FUNCTION)


Let's look at some Oracle CUME_DIST function examples and explore
how to use the CUME_DIST function in Oracle/PLSQL.

For example:

select CUME_DIST(1000, 500) WITHIN GROUP (ORDER BY salary,


bonus)

from employees;

The SQL statement above would return the cumulative distribution of an


employee with a salary of $1,000 and a bonus of $500 from within the
employees table.

CUME_DIST FUNCTION SYNTAX #2 - USED AS


AN ANALYTIC FUNCTION
As an Analytic function, the CUME_DIST function returns the relative
position of a value within a group of values.

The syntax for the CUME_DIST function when used as an Analytic


function is:

CUME_DIST() OVER ( [ query_partition_clause] ORDER BY clause


)

APPLIES TO
The CUME_DIST function can be used in the following versions of
Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g,


Oracle 9i, Oracle 8i

EXAMPLE (AS AN ANALYTIC FUNCTION)


Let's look at some Oracle CUME_DIST function examples and explore
how to use the CUME_DIST function in Oracle/PLSQL.

For example:

select employee_name, salary,

CUME_DIST() OVER (PARTITION BY department ORDER BY salary)

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.

The DENSE_RANK function can be used two ways - as an Aggregate


function or as an Analytic function.

DENSE_RANK FUNCTION SYNTAX #1 - USED AS


AN AGGREGATE FUNCTION
As an Aggregate function, the DENSE_RANK function returns the dense
rank of a row within a group of rows.

The syntax for the DENSE_RANK function when used as an Aggregate


function is:

DENSE_RANK( expression1, ... expression_n ) WITHIN GROUP (


ORDER BY expression1, ... expression_n )

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 DENSE_RANK function can be used in the following versions of
Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE (AS AN AGGREGATE FUNCTION)


Let's look at some Oracle DENSE_RANK function examples and explore
how to use the DENSE_RANK function in Oracle/PLSQL.

For example:

select DENSE_RANK(1000, 500) WITHIN GROUP (ORDER BY salary,


bonus)

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.

DENSE_RANK FUNCTION SYNTAX #2 - USED AS


AN ANALYTIC FUNCTION
As an Analytic function, the DENSE_RANK function returns the rank of
each row of a query with respective to the other rows.

The syntax for the DENSE_RANK function when used as an Analytic


function is:

DENSE_RANK() OVER ( [ query_partition_clause] ORDER BY


clause )

APPLIES TO
The DENSE_RANK function can be used in the following versions of
Oracle/PLSQL:

Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (AS AN ANALYTIC FUNCTION)


Let's look at some Oracle DENSE_RANK function examples and explore
how to use the DENSE_RANK function in Oracle/PLSQL.

For example:

select employee_name, salary,

DENSE_RANK() OVER (PARTITION BY department ORDER BY salary)

from employees

where department = 'Marketing';

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.

ORACLE/PLSQL: EXP FUNCTION


Learn how to use the Oracle/PLSQL EXP function with syntax and
examples.

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

number is the power to raise e to.

APPLIES TO
The EXP 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 EXP function examples and explore how to use
the EXP function in Oracle/PLSQL.

For example:

EXP(3) would return 20.0855369231877

EXP(3.1) would return 22.1979512814416

EXP(-3) would return 0.0497870683678639

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 (

{ YEAR | MONTH | DAY | HOUR | MINUTE | SECOND }

| { TIMEZONE_HOUR | TIMEZONE_MINUTE }

| { TIMEZONE_REGION | TIMEZONE_ABBR }

FROM { date_value | interval_value } )

NOTE
You can only extract YEAR, MONTH, and DAY from a DATE.

You can only extract TIMEZONE_HOUR and TIMEZONE_MINUTE from a


timestamp with a time zone datatype.

APPLIES TO
The EXTRACT 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 EXTRACT function examples and explore how
to use the EXTRACT function in Oracle/PLSQL.

For example:

EXTRACT(YEAR FROM DATE '2003- would return


08-22') 2003

EXTRACT(MONTH FROM DATE '2003- would return 8


08-22')

EXTRACT(DAY FROM DATE '2003-08- would return


22') 22

ORACLE/PLSQL: FLOOR FUNCTION


Learn how to use the Oracle/PLSQL FLOOR function with syntax and
examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g,


Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle FLOOR function examples and explore how to
use the FLOOR function in Oracle/PLSQL.

For example:

FLOOR(5.9) would return 5

FLOOR(34.29) would return 34

FLOOR(-5.9) would return -6

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:

GREATEST( expr1, expr2, ... expr_n )

PARAMETERS OR ARGUMENTS

expr1, expr2, . expr_n are expressions that are evaluated by the


GREATEST 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 greater than another if it has a higher character set value.

APPLIES TO
The GREATEST 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 GREATEST function examples and explore how
to use the GREATEST function in Oracle/PLSQL.

For example:

GREATEST(2, 5, 12, 3) would return 12

GREATEST('2', '5', '12', '3') would return '5'

GREATEST('apples', 'oranges', would return


'bananas') 'oranges'

GREATEST('apples', 'applis', would return


'applas') 'applis'
ORACLE/PLSQL: LEAST FUNCTION
Learn how to use the Oracle/PLSQL LEAST function with syntax and
examples.

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:

LEAST( expr1, expr2, ... expr_n )

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:

Oracle 12c, Oracle 11g, Oracle 10g,


Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LEAST function examples and explore how to
use the LEAST function in Oracle/PLSQL.

For example:

LEAST(2, 5, 12, 3) would return 2

LEAST('2', '5', '12', '3') would return


'12'

LEAST('apples', 'oranges', would return


'bananas') 'apples'

LEAST('apples', 'applis', would return


'applas') 'applas'

LEAST('apples', 'applis', would return


'applas', null) NULL

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

number must be greater than 0.

APPLIES TO
The LN 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 LN function examples and explore how to use the LN function in
Oracle/PLSQL.

For example:

LN(20) would return 2.99573227355399

LN(25) would return 3.2188758248682

LN(100) would return 4.60517018598809

LN(100.5) would return 4.61015772749913

ORACLE/PLSQL: LOG FUNCTION


Learn how to use the Oracle/PLSQL LOG function with syntax and examples.

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

m must be a positive number, except 0 or 1.

n must be a positive number.

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:

LOG(10, 20) would return 1.30102999566398

LOG(2, 15) would return 3.90689059560852

LOG(100, 1) would return 0

ORACLE/PLSQL: MAX FUNCTION


Learn how to use the Oracle/PLSQL MAX function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL MAX function returns the maximum value of an expression.

SYNTAX
The syntax for the Oracle/PLSQL MAX function is:

SELECT MAX( expression )

FROM tables

WHERE conditions;

PARAMETERS OR ARGUMENTS

expression can be a numeric field or formula.

APPLIES TO
The MAX function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i


EXAMPLE - WITH SINGLE FIELD
Let's look at some Oracle MAX function examples and explore how to use the MAX function
in Oracle/PLSQL.

For example, you might wish to know how the maximum salary of all employees.

SELECT MAX(salary) AS "Highest Salary"

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.

EXAMPLE - USING GROUP BY


In some cases, you will be required to use the GROUP BY clause with the MAX function.

For example, you could also use the MAX function to return the name of the department and
the maximum salary in the department.

SELECT department, MAX(salary) AS "Highest salary"

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.

FREQUENTLY ASKED QUESTIONS


Question: I'm trying to pull some info out of a table. To simplify, let's say the table
(report_history) has 4 columns: user_name, report_job_id, report_name, and
report_run_date.

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:

SELECT report_name, MAX(report_run_date)

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:

SELECT rh.user_name, rh.report_name, rh.report_run_date

FROM report_history rh,

(SELECT MAX(report_run_date) AS maxdate, report_name

FROM report_history

GROUP BY report_name) maxresults

WHERE rh.report_name = maxresults.report_name

AND rh.report_run_date= maxresults.maxdate;

Let's take a few moments to explain what we've done.

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:

(SELECT MAX(report_run_date) AS maxdate, report_name

FROM report_history

GROUP BY report_name) maxresults

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 (SELECT customer, SUM(orders.amount) AS total_amt

FROM orders

GROUP BY orders.customer) query1,

(SELECT MAX(query2.total_amt) AS highest_amt

FROM (SELECT customer, SUM(orders.amount) AS total_amt

FROM orders

GROUP BY orders.customer) query2) query3

WHERE query1.total_amt = query3.highest_amt;

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.

Answer: The following SQL SELECT statement should work:

SELECT Name, Score

FROM Scoring

WHERE Score = (SELECT MAX(Score) FROM Scoring);


Question: I need help in a SQL query. I have a table in Oracle called cust_order which has
the following fields: OrderNo, Customer_id, Order_Date, and Amount.

I would like to find the customer_id, who has Highest order count.

I tried with following query.

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 (SELECT Customer_id, Count(*) AS order_count

FROM cust_order

GROUP BY cust_order.Customer_id) query1,

(SELECT max(query2.order_count) AS highest_count

FROM (SELECT Customer_id, Count(*) AS order_count

FROM cust_order

GROUP BY cust_order.Customer_id) query2) query3

WHERE query1.order_count = query3.highest_count;

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

WHERE salary = (SELECT MAX(salary)

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.

Try using this SQL SELECT statement:

SELECT *

FROM employees

WHERE department_id=30

AND salary = (SELECT MAX(salary)

FROM employees

WHERE department_id=30);

This will return the employee information for only the employee in department 30 that has the
highest salary.

ORACLE/PLSQL: MEDIAN FUNCTION


Learn how to use the Oracle/PLSQL MEDIAN function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL MEDIAN function returns the median of an expression.

SYNTAX
The syntax for the Oracle/PLSQL MEDIAN function is:

MEDIAN( expression ) [ OVER ( query partition clause ) ]

PARAMETERS OR ARGUMENTS

expression is the value used to calculate the median.

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:

Oracle 12c, Oracle 11g, Oracle 10g

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

where department = 'Marketing';

The SQL statement above would return the median salary for all employees in the Marketing
department.

ORACLE/PLSQL: MIN FUNCTION


Learn how to use the Oracle/PLSQL MIN function with syntax and examples.

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

expression can be a numeric field or formula.

APPLIES TO
The MIN function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE - WITH SINGLE FIELD


Let's look at some Oracle MIN function examples and explore how to use the MIN function in
Oracle/PLSQL.

For example, you might wish to know how the minimum salary of all employees.

SELECT MIN(salary) AS "Lowest Salary"

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.

EXAMPLE - USING GROUP BY


In some cases, you will be required to use the GROUP BY clause with the MIN function.

For example, you could also use the MIN function to return the name of the department and
the minimum salary in the department.

SELECT department, MIN(salary) AS "Lowest salary"

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

The MOD is calculated as:

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.

The MOD function returns m if n is 0.

APPLIES TO
The MOD 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 MOD function examples and explore how to use the MOD function
in Oracle/PLSQL.

For example:

MOD(15, 4) would return 3

MOD(15, 0) would return 15


MOD(11.6, 2) would return 1.6

MOD(11.6, 2.1) would return 1.1

MOD(-15, 4) would return -3

MOD(-15, 0) would return -15

ORACLE/PLSQL: POWER FUNCTION


Learn how to use the Oracle/PLSQL POWER function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle POWER function examples and explore how to use the POWER
function in Oracle/PLSQL.
For example:

POWER(3, 2) would return 9

POWER(5, 3) would return 125

POWER(-5, 3) would return -125

POWER(6.2, 3) would return 238.328

POWER(6.2, 3.5) would return 593.431934277892

ORACLE/PLSQL: RANK FUNCTION


Learn how to use the Oracle/PLSQL RANK function with syntax and examples.

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.

RANK FUNCTION SYNTAX #1 - USED AS AN AGGREGATE


FUNCTION
As an Aggregate function, the RANK function returns the rank of a row within a group of
rows.

The syntax for the RANK function when used as an Aggregate function is:

RANK( expression1, ... expression_n ) WITHIN GROUP ( ORDER BY expression1,


... expression_n )

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE (AS AN AGGREGATE FUNCTION)


Let's look at some Oracle RANK function examples and explore how to use the RANK
function in Oracle/PLSQL.

For example:

select RANK(1000, 500) WITHIN GROUP (ORDER BY salary, bonus)

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.

RANK FUNCTION SYNTAX #2 - USED AS AN ANALYTIC


FUNCTION
As an Analytic function, the RANK function returns the rank of each row of a query with
respective to the other rows.

The syntax for the RANK function when used as an Analytic function is:

rank() OVER ( [ query_partition_clause] ORDER BY clause )

APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:

Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (AS AN ANALYTIC FUNCTION)

select employee_name, salary,


RANK() OVER (PARTITION BY department ORDER BY salary)

from employees

where department = 'Marketing';

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.

ORACLE/PLSQL: REMAINDER FUNCTION


Learn how to use the Oracle/PLSQL REMAINDER function with syntax and examples.

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

The REMAINDER is calculated as:

m - (n * X) where X is the integer nearest m / n

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:

Oracle 12c, Oracle 11g, Oracle 10g

EXAMPLE
Let's look at some Oracle REMAINDER function examples and explore how to use the
REMAINDER function in Oracle/PLSQL.

For example:

REMAINDER(15, 6) would return 3

REMAINDER(15, 5) would return 0

REMAINDER(15, 4) would return -1

REMAINDER(11.6, 2) would return -0.4

REMAINDER(11.6, 2.1) would return -1

REMAINDER(-15, 4) would return 1

ORACLE/PLSQL: ROUND FUNCTION (WITH


NUMBERS)
Learn how to use the Oracle/PLSQL ROUND function (as it applies to numeric values) with
syntax and examples.

DESCRIPTION
The Oracle/PLSQL ROUND function returns a number rounded to a certain number of
decimal places.

SYNTAX (WITH NUMBERS)


The syntax for the Oracle/PLSQL ROUND function is:

ROUND( number, [ decimal_places ] )

PARAMETERS OR ARGUMENTS

number is the number to round.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE - WITH NUMBERS


Let's look at some Oracle ROUND function examples and explore how to use the ROUND
function in Oracle/PLSQL.

For example:

ROUND(125.315) would return 125

ROUND(125.315, 0) would return 125

ROUND(125.315, 1) would return 125.3

ROUND(125.315, 2) would return 125.32

ROUND(125.315, 3) would return 125.315

ROUND(-125.315, 2) would return -125.32

ORACLE/PLSQL: SIGN FUNCTION


Learn how to use the Oracle/PLSQL SIGN function with syntax and examples.

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.

If number = 0, then sign returns 0.

If number > 0, then sign returns 1.

APPLIES TO
The SIGN 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 SIGN function examples and explore how to use the SIGN function
in Oracle/PLSQL.

For example:

SIGN(-23) would return -1

SIGN(-0.001) would return -1

SIGN(0) would return 0

SIGN(0.001) would return 1

SIGN(23) would return 1

SIGN(23.601) would return 1

ORACLE/PLSQL: SIN FUNCTION


Learn how to use the Oracle/PLSQL SIN function with syntax and examples.

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

n is a number. It is an angle expressed in radians.

APPLIES TO
The SIN 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 SIN function examples and explore how to use the SIN function in
Oracle/PLSQL.

For example:

SIN(3) would return 0.141120008059867

SIN(5.2) would return -0.883454655720153

SIN(-5.2) would return 0.883454655720153

ORACLE/PLSQL: SINH FUNCTION


Learn how to use the Oracle/PLSQL SINH function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle SINH function examples and explore how to use the SINH function
in Oracle/PLSQL.

For example:

SINH(3) would return 10.0178749274099

SINH(5.2) would return 90.6333626553652

SINH(-5.2) would return -90.6333626553652

ORACLE/PLSQL: SQRT FUNCTION


Learn how to use the Oracle/PLSQL SQRT function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle SQRT function examples and explore how to use the SQRT
function in Oracle/PLSQL.

For example:

SQRT(9) would return 3

SQRT(37) would return 6.08276253029822

SQRT(5.617) would return 2.37002109695251

ORACLE/PLSQL: STDDEV FUNCTION


Learn how to use the Oracle/PLSQL STDDEV function with syntax and examples.

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.

STDDEV FUNCTION SYNTAX #1 - USED AS AN AGGREGATE


FUNCTION
The syntax for the STDDEV function when used as an Aggregate function is:

stddev( [ DISTINCT | ALL ] expression )

PARAMETERS OR ARGUMENTS

expression is a numeric value or formula.

APPLIES TO
The STDDEV function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (AS AN AGGREGATE FUNCTION)


Let's look at some Oracle STDDEV function examples and explore how to use the STDDEV
function in 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.

STDDEV FUNCTION SYNTAX #2 - USED AS AN ANALYTIC


FUNCTION
The syntax for the STDDEV function when used as an Analytic function is:

STDDEV( [ DISTINCT | ALL ] expression ) [ OVER ( analytical_clause ) ]

APPLIES TO
The STDDEV function can be used in the following versions of Oracle/PLSQL:

Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (AS AN ANALYTIC FUNCTION)

select employee_name, bonus,

STDDEV(bonus) OVER (ORDER BY salary)

from employees

where department = 'Marketing';

The SQL statement above would return the cumulative standard deviation of the bonuses in
the Marketing department in the employees table, ordered by salary.

ORACLE/PLSQL: SUM FUNCTION


Learn how to use the Oracle/PLSQL SUM function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL SUM function returns the summed value of an expression.

SYNTAX
The syntax for the Oracle/PLSQL SUM function is:

SELECT SUM( expression )

FROM tables

WHERE conditions;

PARAMETERS OR ARGUMENTS

expression can be a numeric field or formula.

APPLIES TO
The SUM function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE - WITH SINGLE FIELD


Let's look at some Oracle SUM function examples and explore how to use the SUM function
in Oracle/PLSQL.

For example, you might wish to know how the combined total salary of all employees whose
salary is above $50,000 / year.

SELECT SUM(salary) AS "Total Salary"

FROM employees

WHERE salary > 50000;

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.

EXAMPLE - USING DISTINCT


You can use the DISTINCT clause within the SUM function. For example, the SQL statement
below returns the combined total salary of unique salary values where the salary is above
$50,000 / year.

SELECT SUM(DISTINCT salary) AS "Total Salary"

FROM employees

WHERE salary > 50000;

If there were two salaries of $82,000/year, only one of these values would be used in the
SUM function.

EXAMPLE - USING FORMULA


The expression contained within the SUM function does not need to be a single field. You
could also use a formula. For example, you might want to calculate the total commission.

SELECT SUM(sales * 0.05) AS "Total Commission"

FROM orders;

EXAMPLE - USING GROUP BY


In some cases, you will be required to use the GROUP BY clause with the SUM function.

For example, you could also use the SUM function to return the name of the department and
the total sales (in the associated department).

SELECT department, SUM(sales) AS "Total sales"

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.

ORACLE/PLSQL: TAN FUNCTION


Learn how to use the Oracle/PLSQL TAN function with syntax and examples.

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

n is a number. It is an angle expressed in radians.

APPLIES TO
The TAN 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 TAN function examples and explore how to use the TAN function in
Oracle/PLSQL.

For example:

TAN(3) would return -0.142546543074278

TAN(5.2) would return -1.88564187751976

TAN(-5.2) would return 1.88564187751976

ORACLE/PLSQL: TANH FUNCTION


Learn how to use the Oracle/PLSQL TANH function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle TANH function examples and explore how to use the TANH
function in Oracle/PLSQL.

For example:

TANH(3) would return 0.99505475368673

TANH(5.2) would return 0.999939136886199

TANH(-5.2) would return -0.999939136886199

ORACLE/PLSQL: TO_NUMBER FUNCTION


Learn how to use the Oracle/PLSQL TO_NUMBER function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_NUMBER function converts a string to a number.

SYNTAX
The syntax for the Oracle/PLSQL TO_NUMBER function is:

TO_NUMBER( string1, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a number.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

TO_NUMBER('1210.73', would return the number


'9999.99') 1210.73

TO_NUMBER('546', '999') would return the number 546

TO_NUMBER('23', '99') would return the number 23

Since the format_mask and nls_language parameters are optional, you can simply convert a
text string to a numeric value as follows:

TO_NUMBER('1210.73') would return the number 1210.73

ORACLE/PLSQL: TRUNC FUNCTION (WITH


NUMBERS)
Learn how to use the Oracle/PLSQL TRUNC function (as it applies to numeric values) with
syntax and examples.

DESCRIPTION
The Oracle/PLSQL TRUNC function returns a number truncated to a certain number of
decimal places.

SYNTAX (WITH NUMBERS)


The syntax for the Oracle/PLSQL TRUNC function is:
TRUNC( number, [ decimal_places ] )

PARAMETERS OR ARGUMENTS

number is the number to truncate.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (WITH NUMBERS)


Let's look at some Oracle TRUNC function examples and explore how to use the TRUNC
function in Oracle/PLSQL.

For example:

TRUNC(125.815) would return 125

TRUNC(125.815, 0) would return 125

TRUNC(125.815, 1) would return 125.8

TRUNC(125.815, 2) would return 125.81

TRUNC(125.815, 3) would return 125.815

TRUNC(-125.815, 2) would return -125.81

TRUNC(125.815, -1) would return 120

TRUNC(125.815, -2) would return 100

TRUNC(125.815, -3) would return


ORACLE/PLSQL: VARIANCE FUNCTION
Learn how to use the Oracle/PLSQL VARIANCE function with syntax and examples.

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

expression is a numeric expression.

APPLIES TO
The VARIANCE 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 VARIANCE function examples and explore how to use the
VARIANCE function in Oracle/PLSQL.

For example:

select VARIANCE(char_length)

from all_tab_columns;

ORACLE/PLSQL: VAR_POP FUNCTION


Learn how to use the Oracle/PLSQL VAR_POP function with syntax and examples.
DESCRIPTION
The Oracle/PLSQL VAR_POP function returns the population variance of a set of numbers.

SYNTAX
The syntax for the Oracle/PLSQL VAR_POP function is:

VAR_POP( expression )

PARAMETERS OR ARGUMENTS

expression is a numeric expression.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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;

ORACLE/PLSQL: VAR_SAMP FUNCTION


Learn how to use the Oracle/PLSQL VAR_SAMP function with syntax and examples.

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

expression is a numeric expression.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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;

ORACLE/PLSQL: ADD_MONTHS FUNCTION


Learn how to use the Oracle/PLSQL ADD_MONTHS function with syntax and examples.

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).

n is the number of months to add to date1.

APPLIES TO
The ADD_MONTHS 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 ADD_MONTHS function examples and explore how to use the
ADD_MONTHS function in Oracle/PLSQL.

For example:

ADD_MONTHS('01-Aug-03', 3) would return '01-Nov-03'

ADD_MONTHS('01-Aug-03', -3) would return '01-May-03'

ADD_MONTHS('21-Aug-03', -3) would return '21-May-03'

ADD_MONTHS('31-Jan-03', 1) would return '28-Feb-03'

ORACLE/PLSQL: CURRENT_DATE FUNCTION


Learn how to use the Oracle/PLSQL CURRENT_DATE function with syntax and examples.

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

There are no parameters or arguments for the CURRENT_DATE function.


APPLIES TO
The CURRENT_DATE function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE
The CURRENT_DATE function can be used in Oracle/PLSQL.

If the following ALTER SESSION command was issued:

ALTER SESSION SET TIME_ZONE = '-7:0';

And then the following SQL statement was executed:

select CURRENT_DATE

from dual;

You might get the following result:

9/10/2005 10:58:24 PM

You then modified the session time zone with the following ALTER SESSION command:

ALTER SESSION SET TIME_ZONE = '-2:0';

And then the following SQL statement was executed:

select CURRENT_DATE

from dual;

You would now get the following result:

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

There are no parameters or arguments for the CURRENT_TIMESTAMP function.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE
The CURRENT_TIMESTAMP function can be used in Oracle/PLSQL.

If the following ALTER SESSION command was issued:

ALTER SESSION SET TIME_ZONE = '-7:0';

And then the following SQL statement was executed:

select CURRENT_TIMESTAMP
from dual;

You might get the following result:

10-Sep-05 10.58.24.853421 PM -07:00

You then modified the session time zone with the following ALTER SESSION command:

ALTER SESSION SET TIME_ZONE = '-2:0';

And then the following SQL statement was executed:

select CURRENT_TIMESTAMP

from dual;

You would now get the following result:

10-Sep-05 03.58.24.853421 AM -02:00

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.

ORACLE/PLSQL: DBTIMEZONE FUNCTION


Learn how to use the Oracle/PLSQL DBTIMEZONE function with syntax and examples.

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

There are no parameters or arguments for the DBTIMEZONE function.

APPLIES TO
The DBTIMEZONE 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 DBTIMEZONE function examples and explore how to use the
DBTIMEZONE function in Oracle/PLSQL.

For example:

select DBTIMEZONE

from dual;

The DBTIMEZONE function might return a value like this:

+00:00

or a value such as this:

-07:00

The value returned depends on the time zone value set in your individual Oracle database.

ORACLE/PLSQL: FROM_TZ FUNCTION


Learn how to use the Oracle/PLSQL FROM_TZ function with syntax and examples.

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:

FROM_TZ( timestamp_value, time_zone_value )

PARAMETERS OR ARGUMENTS

timestamp_value is the value to convert to a TIMESTAMP WITH TIME ZONE value.

time_zone_value is the time zone used to convert timestamp_value to a TIMESTAMP WITH


TIME ZONE value.
APPLIES TO
The FROM_TZ 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 FROM_TZ function examples and explore how to use the
FROM_TZ function in Oracle/PLSQL.

For example:

If you executed the following SQL statement:

select FROM_TZ(TIMESTAMP '2005-09-11 01:50:42', '5:00')

from dual;

You would now get the following result:

11-Sep-05 01.50.42.000000 AM +05:00

If you executed the same SQL statement, but set the time zone to '-7:00' as follows:

select FROM_TZ(TIMESTAMP '2005-09-11 01:50:42', '-7:00')

from dual;

You would now get the following result:

11-Sep-05 01.50.42.000000 AM -07:00

ORACLE/PLSQL: LAST_DAY FUNCTION


Learn how to use the Oracle/PLSQL LAST_DAY function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

LAST_DAY(TO_DATE('2003/03/15', would return Mar 31,


'yyyy/mm/dd')) 2003

LAST_DAY(TO_DATE('2003/02/03', would return Feb 28,


'yyyy/mm/dd')) 2003

LAST_DAY(TO_DATE('2004/02/03', would return Feb 29,


'yyyy/mm/dd')) 2004

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

There are no parameters or arguments for the LOCALTIMESTAMP function.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE
Let's look at some Oracle LOCALTIMESTAMP function examples and explore how to use the
LOCALTIMESTAMP function in Oracle/PLSQL.

For example:

If the following ALTER SESSION command was issued:

ALTER SESSION SET TIME_ZONE = '-7:0';

And then the following SQL statement was executed:

select LOCALTIMESTAMP

from dual;

You might get the following result:

10-Sep-05 10.58.24 PM

You then modified the session time zone with the following ALTER SESSION command:

ALTER SESSION SET TIME_ZONE = '-2:0';

And then the following SQL statement was executed:


select LOCALTIMESTAMP

from dual;

You would now get the following result:

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:

MONTHS_BETWEEN( date1, date2 )

PARAMETERS OR ARGUMENTS

date1 and date2 are the dates used to calculate the number of months.

If a fractional month is calculated, the MONTHS_BETWEEN function calculates the fraction


based on a 31-day month.

APPLIES TO
The MONTHS_BETWEEN 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 MONTHS_BETWEEN function examples and explore how to use
the MONTHS_BETWEEN function in Oracle/PLSQL.
For example:

MONTHS_BETWEEN (TO_DATE ('2003/01/01', 'yyyy/mm/dd'), TO_DATE


('2003/03/14', 'yyyy/mm/dd') )

would return -2.41935483870968

Another example using the MONTHS_BETWEEN function in Oracle/PLSQL is:

MONTHS_BETWEEN (TO_DATE ('2003/07/01', 'yyyy/mm/dd'), TO_DATE


('2003/03/14', 'yyyy/mm/dd') )

would return 3.58064516129032

Another example using the MONTHS_BETWEEN function is:

MONTHS_BETWEEN (TO_DATE ('2003/07/02', 'yyyy/mm/dd'), TO_DATE


('2003/07/02', 'yyyy/mm/dd') )

would return 0

A final example using the MONTHS_BETWEEN function in Oracle/PLSQL is:

MONTHS_BETWEEN (TO_DATE ('2003/08/02', 'yyyy/mm/dd'), TO_DATE


('2003/06/02', 'yyyy/mm/dd') )

would return 2

ORACLE/PLSQL: NEW_TIME FUNCTION


Learn how to use the Oracle/PLSQL NEW_TIME function with syntax and examples.

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:

NEW_TIME( date, zone1, zone2 )

PARAMETERS OR ARGUMENTS
zone1 and zone2 can be any of the following values:

Value Description

AST Atlantic Standard Time

ADT Atlantic Daylight Time

BST Bering Standard Time

BDT Bering Daylight Time

CST Central Standard Time

CDT Central Daylight Time

EST Eastern Standard Time

EDT Eastern Daylight Time

GMT Greenwich Mean Time

HST Alaska-Hawaii Standard Time

HDT Alaska-Hawaii Daylight Time

MST Mountain Standard Time

MDT Mountain Daylight Time

NST Newfoundland Standard Time

PST Pacific Standard Time


PDT Pacific Daylight Time

YST Yukon Standard Time

YDT Yukon Daylight Time

APPLIES TO
The NEW_TIME 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 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:

NEW_TIME (TO_DATE ('2003/11/01 01:45', 'yyyy/mm/dd HH24:MI'), 'AST', 'MST')

This example would return '2003/10/31 10:45:00 PM'.

ORACLE/PLSQL: NEXT_DAY FUNCTION


Learn how to use the Oracle/PLSQL NEXT_DAY function with syntax and examples.

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:

NEXT_DAY( date, weekday )

PARAMETERS OR ARGUMENTS
date is used to find the next weekday.

weekday is a day of the week (ie: SUNDAY, MONDAY, TUESDAY, WEDNESDAY,


THURSDAY, FRIDAY, SATURDAY)

APPLIES TO
The NEXT_DAY 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 NEXT_DAY function examples and explore how to use the
NEXT_DAY function in Oracle/PLSQL.

For example:

NEXT_DAY('01-Aug-03', 'TUESDAY') would return '05-Aug-03'

NEXT_DAY('06-Aug-03', 'WEDNESDAY') would return '13-Aug-03'

NEXT_DAY('06-Aug-03', 'SUNDAY') would return '10-Aug-03'

ORACLE/PLSQL: ROUND FUNCTION (WITH


DATES)
Learn how to use the Oracle/PLSQL ROUND function (as it applies to date values) with
syntax and examples.

DESCRIPTION
The Oracle/PLSQL ROUND function returns a date rounded to a specific unit of measure.

ROUND FUNCTION SYNTAX (WITH DATES)


The syntax for the Oracle/PLSQL ROUND function is:

ROUND( date, [ format ] )

PARAMETERS OR ARGUMENTS

date is the date to round.


format is the unit of measure to apply for rounding. If the format parameter is omitted, the
ROUND function will round to the nearest day.

Below are the valid format parameters:

Unit Valid format parameters Rounding Rule

Year SYYYY, YYYY, YEAR, SYEAR, Rounds up on July 1st


YYY, YY, Y

ISO Year IYYY, IY, I

Quarter Q Rounds up on the 16th day of the second


month of the quarter

Month MONTH, MON, MM, RM Rounds up on the 16th day of the month

Week WW Same day of the week as the first day of


the year

IW IW Same day of the week as the first day of


the ISO year

W W Same day of the week as the first day of


the month

Day DDD, DD, J

Start day of the DAY, DY, D


week

Hour HH, HH12, HH24

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

EXAMPLE - WITH DATES


Let's look at some Oracle ROUND function examples and explore how to use the ROUND
function in Oracle/PLSQL.

For example:

ROUND(TO_DATE ('22-AUG-03'),'YEAR') would return '01-JAN-04'

ROUND(TO_DATE ('22-AUG-03'),'Q') would return '01-OCT-03'

ROUND(TO_DATE ('22-AUG-03'),'MONTH') would return '01-SEP-03'

ROUND(TO_DATE ('22-AUG-03'),'DDD') would return '22-AUG-03'

ROUND(TO_DATE ('22-AUG-03'),'DAY') would return '24-AUG-03'

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

There are no parameters or arguments for the SESSIONTIMEZONE function.

APPLIES TO
The SESSIONTIMEZONE 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 SESSIONTIMEZONE function examples and explore how to use
the SESSIONTIMEZONE function in Oracle/PLSQL.

For example:

SELECT SESSIONTIMEZONE

FROM dual;

The SESSIONTIMEZONE function might return a value like this:

+00:00

or a value such as this:

-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:

ALTER SESSION SET TIME_ZONE = '-7:0';

ORACLE/PLSQL: SYSDATE FUNCTION


Learn how to use the Oracle/PLSQL SYSDATE function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

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:

select supplier_id, SYSDATE

from suppliers

where supplier_id > 5000;

If you wanted to extract the date portion only (and exclude the time component), you could
use the TO_CHAR function. For example:

select supplier_id, TO_CHAR(SYSDATE, 'yyyy/mm/dd')

from suppliers

where supplier_id > 5000;

ORACLE/PLSQL: SYSTIMESTAMP FUNCTION


Learn how to use the Oracle/PLSQL SYSTIMESTAMP function with syntax and examples.

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

There are no parameters or arguments for the SYSTIMESTAMP function.

APPLIES TO
The SYSTIMESTAMP 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 SYSTIMESTAMP function examples and explore how to use the
SYSTIMESTAMP function in Oracle/PLSQL.

For example:

SELECT SYSTIMESTAMP

FROM dual;

The systimestamp function might return a value like this:

03-11-21 20:4

You can also choose to use the TO_CHAR function with the SYSTIMESTAMP function. For
example:

SELECT TO_CHAR(SYSTIMESTAMP, 'SSSS.FF')

FROM dual;

The function above may return a value such as:

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:

TO_CHAR( value, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.

nls_language is optional. This is the nls language used to convert value to a string.

APPLIES TO
The TO_CHAR 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 TO_CHAR function examples and explore how to use the
TO_CHAR function in Oracle/PLSQL.

WITH NUMBERS

For example:

The following are number examples for the TO_CHAR function.

TO_CHAR(1210.73, '9999.9') would return ' 1210.7'

TO_CHAR(-1210.73, '9999.9') would return '-1210.7'


TO_CHAR(1210.73, '9,999.99') would return ' 1,210.73'

TO_CHAR(1210.73, '$9,999.00') would return ' $1,210.73'

TO_CHAR(21, '000099') would return ' 000021'

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

YEAR Year, spelled out

YYYY 4-digit year

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).


WW Week of year (1-53) where week 1 starts on the first day of the year
and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the month
and ends on the seventh.

IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds.

The following are date examples for the TO_CHAR function.


TO_CHAR(sysdate, 'yyyy/mm/dd') would return '2003/07/09'

TO_CHAR(sysdate, 'Month DD, would return 'July 09, 2003'


YYYY')

TO_CHAR(sysdate, 'FMMonth DD, would return 'July 9, 2003'


YYYY')

TO_CHAR(sysdate, 'MON DDth, would return 'JUL 09TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMON DDth, would return 'JUL 9TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMon ddth, would return 'Jul 9th, 2003'


YYYY')

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.

TO_CHAR(sysdate, 'FMMonth DD, would return 'July 9, 2003'


YYYY')

TO_CHAR(sysdate, 'FMMON DDth, would return 'JUL 9TH,


YYYY') 2003'

TO_CHAR(sysdate, 'FMMon ddth, would return 'Jul 9th, 2003'


YYYY')

The zeros have been suppressed so that the day component shows as "9" as opposed to
"09".

FREQUENTLY ASKED QUESTIONS


Question: Why doesn't this sort the days of the week in order?

SELECT ename, hiredate, TO_CHAR((hiredate),'fmDay') "Day"


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:

SELECT ename, hiredate, TO_CHAR((hiredate),'fmD') "Day"

FROM emp

ORDER BY "Day";

ORACLE/PLSQL: TO_DATE FUNCTION


Learn how to use the Oracle/PLSQL TO_DATE function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_DATE function converts a string to a date.

SYNTAX
The syntax for the Oracle/PLSQL TO_DATE function is:

TO_DATE( string1, [ format_mask ], [ nls_language ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a date.

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

YYYY 4-digit year

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

IYY Last 3, 2, or 1 digit(s) of ISO year.


IY
I

IYYY 4-digit year based on the ISO standard

RRRR Accepts a 2-digit year and returns a 4-digit year.


A value between 0-49 will return a 20xx year.
A value between 50-99 will return a 19xx year.

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the
month and ends on the seventh.

IW Week of year (1-52 or 1-53) based on the ISO standard.


D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).

DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

SSSSS Seconds past midnight (0-86399).

FF Fractional seconds. Use a value from 1 to 9 after FF to indicate


the number of digits in the fractional seconds. For example,
'FF4'.

AM, A.M., PM, Meridian indicator


or P.M.

AD or A.D AD indicator

BC or B.C. BC indicator
TZD Daylight savings information. For example, 'PST'

TZH Time zone hour.

TZM Time zone minute.

TZR Time zone region.

APPLIES TO
The TO_DATE 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 TO_DATE function examples and explore how to use the
TO_DATE function in Oracle/PLSQL.

For example:

TO_DATE('2003/07/09', would return a date value of July


'yyyy/mm/dd') 9, 2003

TO_DATE('070903', 'MMDDYY') would return a date value of July


9, 2003

TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar


15, 2002

ORACLE/PLSQL: TO_TIMESTAMP FUNCTION


Learn how to use the Oracle/PLSQL TO_TIMESTAMP function with syntax and examples.

DESCRIPTION
The Oracle/PLSQL TO_TIMESTAMP function converts a string to a timestamp.

SYNTAX
The syntax for the Oracle/PLSQL TO_TIMESTAMP function is:

TO_TIMESTAMP( string1, [ format_mask ] [ 'nlsparam' ] )

PARAMETERS OR ARGUMENTS

string1 is the string that will be converted to a timestamp.

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

YYYY 4-digit year

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

DD Day of month (1-31).

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

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:

TO_TIMESTAMP('2003/12/13 10:13:18', 'YYYY/MM/DD HH:MI:SS')

would return '13-DEC-03 10.13.18.000000000 AM' as a timestamp value.

TO_TIMESTAMP('2003/DEC/13 10:13:18', 'YYYY/MON/DD HH:MI:SS')

would also return '13-DEC-03 10.13.18.000000000 AM' as a timestamp value.

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:

TO_TIMESTAMP_TZ( string1 , [ format_mask ] [ 'nlsparam' ] )

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

YYYY 4-digit year

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

DD Day of month (1-31).

HH Hour of day (1-12).

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI Minute (0-59).

SS Second (0-59).

TZM Time zone minute.

TZH Time zone hour.

APPLIES TO
The TO_TIMESTAMP_TZ 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_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 return '13-DEC-03 10.13.18.000000000 AM -08:00' as a timestamp with time zone


value.

TO_TIMESTAMP_TZ('2003/DEC/13 10:13:18 -8:00', 'YYYY/MON/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.

ORACLE/PLSQL: TO_YMINTERVAL FUNCTION


Learn how to use the Oracle/PLSQL TO_YMINTERVAL function with syntax and examples.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

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:

TO_YMINTERVAL('03- would return 3 years 11 months as an


11') INTERVAL YEAR TO MONTH type

TO_YMINTERVAL('01- would return 1 year 5 months as an


05') INTERVAL YEAR TO MONTH type

TO_YMINTERVAL('00- would return 0 years 1 month as an


01') INTERVAL YEAR TO MONTH type

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.

select order_date, order_date + to_yminterval('01-05')

from orders;

This SQL statement would return the order date, as well as the order date plus 1 year and 5
months

ORACLE/PLSQL: TRUNC FUNCTION (WITH


DATES)
Learn how to use the Oracle/PLSQL TRUNC function (as it applies to date values) with
syntax and examples.

DESCRIPTION
The Oracle/PLSQL TRUNC function returns a date truncated to a specific unit of measure.

SYNTAX (WITH DATES)


The syntax for the Oracle/PLSQL TRUNC function is:

TRUNC ( date, [ format ] )

PARAMETERS OR ARGUMENTS

date is the date to truncate.

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.

Below are the valid format parameters:


Unit Valid format parameters

Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y

ISO Year IYYY, IY, I

Quarter Q

Month MONTH, MON, MM, RM

Week WW

IW IW

W W

Day DDD, DD, J

Start day of the week DAY, DY, D

Hour HH, HH12, HH24

Minute MI

APPLIES TO
The TRUNC function can be used in the following versions of Oracle/PLSQL:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE - WITH DATES


Let's look at some Oracle TRUNC function examples and explore how to use the TRUNC
function in Oracle/PLSQL.

For example:
TRUNC(TO_DATE('22-AUG-03'), 'YEAR') would return '01-JAN-03'

TRUNC(TO_DATE('22-AUG-03'), 'Q') would return '01-JUL-03'

TRUNC(TO_DATE('22-AUG-03'), 'MONTH') would return '01-AUG-03'

TRUNC(TO_DATE('22-AUG-03'), 'DDD') would return '22-AUG-03'

TRUNC(TO_DATE('22-AUG-03'), 'DAY') would return '17-AUG-03'

ORACLE/PLSQL: TZ_OFFSET FUNCTION


Learn how to use the Oracle/PLSQL TZ_OFFSET function with syntax and examples.

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.

Some of the more commonly used time zone names are:

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:

SELECT DISTINCT tzname

FROM V$TIMEZONE_NAMES;
APPLIES TO
The TZ_OFFSET 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 TZ_OFFSET function examples and explore how to use the
TZ_OFFSET function in Oracle/PLSQL.

For example:

TZ_OFFSET('US/Michigan') would return '-05:00'

TZ_OFFSET('-08:00') would return '-08:00'

TZ_OFFSET(sessiontimezone) would return '-07:00' (depending on your


configuration)

TZ_OFFSET(dbtimezone) would return '-07:00' (depending on your


configuration)

MISCELLANEOUS FUNCTIONS

ORACLE/PLSQL: SQLCODE FUNCTION


Learn how to use the Oracle/PLSQL SQLCODE function with syntax and examples.

WHAT DOES THE SQLCODE FUNCTION DO?


The SQLCODE function returns the error number associated with the most recently raised
error exception. This function should only be used within the Exception Handling section of
your code:

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

WHEN exception_name1 THEN

[statements]

WHEN exception_name2 THEN

[statements]

WHEN exception_name_n THEN

[statements]

WHEN OTHERS THEN

[statements]

END [procedure_name];

You could use the SQLCODE function to raise an error as follows:

EXCEPTION

WHEN OTHERS THEN

raise_application_error(-20001,'An error was encountered -


'||SQLCODE||' -ERROR- '||SQLERRM);
END;

Or you could log the error to a table using the SQLCODE function as follows:

EXCEPTION

WHEN OTHERS THEN

err_code := SQLCODE;

err_msg := SUBSTR(SQLERRM, 1, 200);

INSERT INTO audit_table (error_number, error_message)

VALUES (err_code, err_msg);

END;

ORACLE/PLSQL: SQLERRM FUNCTION


Learn how to use the Oracle/PLSQL SQLERRM function with syntax and examples.

WHAT DOES THE SQLERRM FUNCTION DO?


The SQLERRM function returns the error message associated with the most recently raised
error exception. This function should only be used within the Exception Handling section of
your code.

SYNTAX
The syntax for the Oracle/PLSQL SQLERRM function is:

SQLERRM

PARAMETERS OR ARGUMENTS

There are no parameters or arguments for the SQLERRM function.

NOTE
See also the SQLCODE function.
EXAMPLE
Since EXCEPTION HANDLING is usually written with the following syntax:

EXCEPTION

WHEN exception_name1 THEN

[statements]

WHEN exception_name2 THEN

[statements]

WHEN exception_name_n THEN

[statements]

WHEN OTHERS THEN

[statements]

END [procedure_name];

You could use the SQLERRM function to raise an error as follows:

EXCEPTION

WHEN OTHERS THEN

raise_application_error(-20001,'An error was encountered -


'||SQLCODE||' -ERROR- '||SQLERRM);

END;

Or you could log the error to a table using the SQLERRM function as follows:

EXCEPTION
WHEN OTHERS THEN

err_code := SQLCODE;

err_msg := SUBSTR(SQLERRM, 1, 200);

INSERT INTO audit_table (error_number, error_message)

VALUES (err_code, err_msg);

END;

ORACLE/PLSQL: USER FUNCTION


(RETRIEVING USERNAME)
Learn how to return the user_id in Oracle/PLSQL with syntax and examples.

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

There are no parameters or arguments for the USER function.

APPLIES TO
The USER 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 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.

ORACLE/PLSQL: UID FUNCTION


Learn how to use the Oracle/PLSQL UID function with syntax and examples.

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

There are no parameters or arguments for the UID function.

APPLIES TO
The UID 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 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.

Вам также может понравиться