Professional Documents
Culture Documents
SQL Built-In Function Reference: Numeric Format Elements
SQL Built-In Function Reference: Numeric Format Elements
SQL Built-In Function Reference: Numeric Format Elements
SQL
Built-in
B
A P P E N D I X
✦ ✦ ✦ ✦
Function
Reference
T his appendix describes the many built-in functions that
are available from SQL and PL/SQL. Before presenting the
list of functions, numeric format elements and date format
elements are described. Then the functions are presented in
alphabetical order.
Table B-1
Numeric Format Elements
Element Sample Description
Continued
55
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 956
956 Appendixes
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 957
Table B-2
Date Format Elements
Element Example TO_DATE? Description
Continued
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 958
958 Appendixes
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 959
Continued
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 960
960 Appendixes
In general, date format elements are not case-sensitive. However, when formatting
dates for display, the case does matter when the element being formatted is a text
element. Using the month name as an example, note the effect of case on the
following results:
TO_CHAR(SYSDATE,’MONTH’) = NOVEMBER
TO_CHAR(SYSDATE,’Month’) = November
TO_CHAR(SYSDATE,’month’) = november
When the entire word MONTH is capitalized, the resulting month name is formatted
in all caps. Otherwise, the capitalization of the first letter of the format string
determines whether the first character of the month name is capitalized. These
same rules also apply to the following format elements: AD, AM, PM, BC, DAY, DY,
MON, RM, and YEAR.
Note SQL’s aggregate functions are different in nature from the functions described in
this appendix. The aggregate functions are described in Chapter 16, “Selecting
Data with SQL,” and are not described again here.
ABS
The ABS function returns the absolute value of a number. The absolute value
represents the value with any negative sign removed. Following is its syntax:
ABS(number)
number The number for which you want the absolute value
The ABS function strips the sign off of a number, as shown in this example:
ABS (5) = 5
ABS (-5) = 5
ACOS
The ACOS function returns the arc cosine of a number. The result is expressed
in radians and will be between 0 and pi, inclusively. Following is its syntax:
ACOS(number)
The following examples show the arc cosines of 1 and –1, respectively:
ACOS (1) = 0
ACOS (-1) = 3.1415927 (pi)
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 962
962 Appendixes
ADD_MONTHS
The ADD_MONTHS function adds a specified number of months to a date. The day
of the month is usually left unchanged. However, if the starting date represents the
last day of its month, then the result will be adjusted so that it also represents the
last day of the month. Also, if the resulting month has fewer days than the starting
month, then the day may be adjusted downwards to come up with a valid date.
Following is its syntax:
ADD_MONTHS(date, months)
ADD_MONTHS(TO_DATE(‘15-Nov-1961’,’dd-mon-yyyy’), 1)
= ‘15-Dec-1961
ADD_MONTHS(TO_DATE(‘30-Nov-1961’,’dd-mon-yyyy’), 1)
= ‘31-Dec-1961
ADD_MONTHS(TO_DATE(‘31-Jan-1999’,’dd-mon-yyyy’), 1)
= ‘28-Feb-1999’
Note that in the third example, the day of the month had to be adjusted downwards
from 31 to 28 because February 1999 has only 28 days. In the second example, the
date was adjusted upwards from 30 to 31 because of the need to maintain the date
equal to the last day of the month.
ASCII
The ASCII function returns the decimal representation of the first character of
whatever string you pass to it. This may or may not be an actual ASCII value. If
your database character set is seven-bit ASCII, then you’ll get an ASCII value back.
The value returned will correspond to the character set being used.
ASCII(string)
ASCII(‘J’) = 74
ASCII(‘Jeff’) = 74
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 963
As you can see from the second example, if you pass in a multicharacter string, the
ASCII function ignores all but the first character.
ASIN
The ASIN function returns the arc sine of a number. The result is expressed in
radians and will range in value from –(pi/2) to (pi/2). Following is its syntax:
ASIN(number)
ATAN
The ATAN function returns the arc tangent of a number. The result is expressed
in radians and ranges between –(pi/2) and (pi/2). Following is its syntax:
ATAN(number)
The following examples provide the arc tangents of 1 and –1, respectively:
ATAN2
The ATAN2 function returns the arc tangent of two numbers. The results are
expressed in radians. Following is its syntax:
ATAN2(first, second)
964 Appendixes
The following examples provide the arc tangents of 1 and –1, respectively:
BFILENAME
The BFILENAME function returns a BFILE locator that points to a physical file on
disk. Following is its syntax:
BFILENAME(directory, filename)
BFILENAME(‘gif_dir’, ‘book_photo.gif’)
CEIL
The CEIL function takes a number as input, which could be a noninteger value, and
returns the lowest valued integer greater than or equal to the input value. Following
is its syntax:
CEIL(number)
CEIL(5.1) = 6
CEIL(-5.1) = -5
Note the result of using CEIL on a negative value. The lowest valued integer greater
than or equal to –5.1 is actually –5. Mathematically, that makes sense, but it may
seem counterintuitive at first.
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 965
CHARTOROWID
The CHARTOROWID function converts a character string to a rowid type. Following
is its syntax:
CHARTOROWID(string)
ROWID
------------------
AAAADCAABAAAAVUAAA
D
-
X
CHR
The CHR function returns the character associated with a specific numeric value
according to the database’s character set. For example, given an ASCII value, CHR
could be used to return the corresponding character. Following is its syntax:
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 966
966 Appendixes
CONCAT
The CONCAT function takes two strings as input, combines them together into one
string, and returns the result. Following is its syntax:
CONCAT(string_1, string_2)
Note It’s often easier to concatenate strings using the || operator: for example, ‘This
is ‘ || ‘a test’.
CONVERT
The CONVERT function converts a character string from one character set to
another. Following is its syntax:
CONVERT(‘Jonathan’,’WE8EBCDIC37C’,’US7ASCII’)
CONVERT(‘Jonathan’,’WE8ROMAN8’)
The first example converts from the US7ASCII character set. The second example
converts from the database’s character set.
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 967
COS
The COS function returns the cosine of an angle. The result is expressed in radians.
Following is its syntax:
COS(angle)
Note You can convert an angle from degrees to radians using the following formula:
radians = degrees * pi/180.
COSH
The COSH function returns the hyperbolic cosine of an angle. Following is its syntax:
COSH(angle)
COSH(0) = 1
COSH(90*3.14/180) = 2.507
See the COS function for information on converting from degrees to radians.
DECODE
The DECODE function serves as an inline IF statement. It takes an input value,
compares it to a list of value/result pairs, and returns the result corresponding
to the input value. There is a provision for a default result in case none of the
value/result pairs match. Unlike other SQL functions, the DECODE function can
recognize and operate with null values. Following is its syntax:
DECODE(input_value,
value, result[,
value, result...][,
default_result]);
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 968
968 Appendixes
input_value The value that you want to evaluate. The DECODE function
compares this value to the various value/result pairs to
determine the result to return.
value A value in a value/result pair. If the input value matches
this, then the corresponding result is returned. The NULL
keyword may be used for a value to specify a result for a
null input.
result The result in a value result pair.
default_result A default result that is returned if none of the values in
the value/result pairs matches the input value.
The following example shows how the DECODE flag can be used to supply human
readable terms for the values in the BLOOD_TEST_FLAG column of the SEAPARK
user’s CHECKUP table:
SELECT checkup_type,
DECODE(blood_test_flag,
‘Y’,’Yes’,
‘N’,’No’,
NULL,’None’,
‘Invalid’)
FROM checkup;
This SQL statement demonstrates all the basic functionality of DECODE. The input
to the function is the BLOOD_TEST_FLAG column. If the value of that column is
a ‘Y’, then the function will return ‘Yes’. If the value is ‘N’, the function will
return ‘No’. If the value is NULL, the function will return ‘None’. If none of these
value/pairs is selected, the column is presumed to have an invalid value, and the
word ‘Invalid’ will be returned.
The DECODE function can often be put to some creative uses in a SQL query. One
technique is to turn what would otherwise be rows into columns. Consider the
query in the following example:
TO_C COUNT(*)
---- ---------
1995 1
1996 3
1997 1
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 969
This query tells you how many animals were born in the years 1995, 1996, and 1997.
Each row in the query displays the count for a different year. What if, however, you
really want those values displayed as three columns? You can make that happen
through the creative application of DECODE, as shown in Listing B-1.
In this example, the BIRTH_DATE column is referenced three times to create three
columns in the result set. The DECODE function is applied to each column in such
a way as to filter the dates returned for one specific year. In the first column, for
example, DECODE changes any 1995 dates to a 1 and any non-1995 dates to a 0. The
SUM function then totals up the values to arrive at the number of animals born in
1995. The expressions in the other two columns perform the same process for the
years 1996 and 1997.
DUMP
The DUMP function returns the internal representation of the data stored within
a column or the data returned by an expression. The return value of DUMP is
VARCHAR2. Following is its syntax:
970 Appendixes
The following examples show the DUMP function being used to display the internal
representation of the current date. The first example shows the entire date and
uses decimal notation. The second example begins with the fourth byte and uses
hexadecimal notation (decimal 24 = hexadecimal 18).
DUMP(SYSDATE)
--------------------------------------------
Typ=13 Len=8: 207,7,11,24,22,17,52,0
DUMP(SYSDATE,16,4)
--------------------------------------------
Typ=13 Len=8: 18,16,12,12,0
EMPTY_BLOB
The EMPTY_BLOB function returns an empty BLOB locator that can be used in an
INSERT or UPDATE statement to initialize a BLOB column. Following is its syntax:
EMPTY_BLOB()
This function has no parameters. The following example shows EMPTY_BLOB being
used to initialize a LOB column for a new row being inserted into a table:
EMPTY_CLOB
The EMPTY_CLOB function performs the same function for CLOBs that EMPTY_BLOB
does for BLOBs. It returns an empty CLOB locator that can be used in an INSERT or
UPDATE statement to initialize a CLOB column. Following is its syntax:
EMPTY_CLOB()
This function has no parameters. The following example shows EMPTY_CLOB being
used to initialize a CLOB column for a new row being inserted into a table:
EXP
The EXP function returns the value of e raised to a specific power. In mathematics,
e is used to represent a specific transcendental number (infinite number of decimal
places) that takes the value 2.718..., and forms the basis of natural logarithms.
Following is its syntax:
EXP(exponent)
EXP(1) = 2.7182818
EXP(3) = 20.085537 (2.7182818 * 2.7182818 * 2.7182818)
FLOOR
The FLOOR function takes a number as input, which could be a decimal number, and
returns the largest integer less than or equal to the input value. The FLOOR function
works similarly to CEIL, but in the opposite manner. Following is its syntax:
FLOOR(number)
972 Appendixes
FLOOR(5.1) = 5
FLOOR(-5.1) = -6
To understand how FLOOR and CEIL function when used against negative values,
compare the result of FLOOR(-5.1) with the results of CEIL(-5.1).
GREATEST
The GREATEST function takes a list of values and returns the highest value in that
list. You can use the GREATEST function with either numeric or character-string
values. Following is its syntax:
The third example illustrates what happens when you mix datatypes. Since the
first datatype is a string, Oracle converts the numeric 1 to the string ‘1’ to make
the datatypes match. While the number 101 is greater than the number 11, the
reverse is true when you convert both to strings. In that case, ‘11’ is greater
than ‘101’ and is what the function returns.
Note When used with text values, the return type is always VARCHAR2. That’s true even
if the input values are CHAR.
HEXTORAW
The HEXTORAW function converts a string of hexadecimal digits into a raw value.
Following is its syntax:
HEXTORAW(string)
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 973
The examples shown in Listing B-2 illustrate using the HEXTORAW function:
Table created.
1 row created.
DUMP(Y)
-------------------------------------------------
Typ=23 Len=3: 65,66,67
Notice that the values in the raw column are exactly what was specified in the call
to HEXTORAW (decimal 65 = hexadecimal 41).
INITCAP
The INITCAP function takes a string and changes all the words in that string so that
they start with an initial capital letter. Following is its syntax:
INITCAP(string)
INSTR
The INSTR function tells you whether one string is contained inside another, and if
so, where. Following is its syntax:
974 Appendixes
The INSTR function returns the index at which the substring was found. The
following examples illustrate using the INSTR function:
INSTR(‘AAABAABA’,’B’) = 4
INSTR(‘AAABAABA’,’B’,1,2) = 7
In the second example, the occurrence parameter is 2, so the location of the second
occurrence of ‘B’ is returned by the function.
INSTRB
The INSTRB function tells you whether one string is contained inside another, and,
if so, where. The INSTRB function is the same as INSTR, except that the value it
returns represents a byte index, not a character index. This difference has meaning
only when a multibyte character set is being used. Following is its syntax:
The INSTRB function returns the index at which the substring was found. The
following examples illustrate using the INSTRB function:
INSTRB(‘AAABAABA’,’B’) = 4
INSTRB(‘AAABAABA’,’B’,1,2) = 7
In the second example, the occurrence parameter is 2, so the location of the second
occurrence of ‘B’ is returned by the function.
LAST_DAY
The LAST_DAY function takes a date as input and returns the last date of the
corresponding month. Following is its syntax:
LAST_DAY(date)
LAST_DAY(TO_DATE(‘29-Dec-1988’,’dd-mon-yyyy’))
= ‘31-Dec-1988
LAST_DAY(TO_DATE(‘1-Feb-2000’,’dd-mon-yyyy’))
= ‘29-Feb-2000
LEAST
The LEAST function takes a list of values and returns the lowest value in that list.
You can use the LEAST function with either numeric or character-string values.
Following is its syntax:
value A numeric or text value. Typically, you want all values to be the
same datatype. If you mix datatypes, the value returned will match
the datatype of the first parameter. All other parameters will be
implicitly converted to that datatype as well.
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 976
976 Appendixes
The third example illustrates what happens when you mix datatypes. Since
the first datatype is a string, Oracle converts the numeric 1 to the string ‘1’ to
make the datatypes match. While the number 12 is less than the number 110, the
reverse is true when you convert both to strings. In that case, ‘110’ is less than
‘12’ (based on the sort order), and is what the function returns.
Note When used with text values, the return type is always VARCHAR2. That’s true even
if the input values are CHAR.
LENGTH
The LENGTH function returns the length of a string in terms of the number of
characters that it contains. Following is its syntax:
LENGTH(string)
LENGTH(‘This is short’) = 13
LENGTH(‘This is a bit longer’) = 20
LENGTHB
The LENGTHB function returns the length of a string in terms of the number of bytes
that it contains. Except for when a multibyte character set is being used, LENGTHB
will return the same values as LENGTH. Following is its syntax:
LENGTHB(string)
LENGTHB(‘This is short’) = 13
LENGTHB(‘This is a bit longer’) = 20
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 977
LN
The LN function returns the natural logarithm of a number. The natural logarithm of
a number is the power to which e must be raised to yield that number as a result.
Following is its syntax:
LN(number)
The following examples illustrate how LN functions, and also show its
relationship to EXP:
LN(10) = 2.3025851
EXP(2.3025851) = 10
As you can see, feeding the result of LN through the EXP function should give
you back your original value.
LOG
The LOG function returns the logarithm (not natural) of a number. Following is
its syntax:
LOG(logbase, number)
LOG(10,100) = 2
LOG(EXP(1),10) = 2.3025851 = LN(10)
The value for log base 10 of 100 is 2 because 102 equals 100. Note that LOG(e, x)
is the equivalent of LN(x). You can use EXP(1) to get the value of e, as shown in
the second example.
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 978
978 Appendixes
LOWER
The LOWER function returns the lowercase version of a string. Following is
its syntax:
LOWER(string)
LPAD
The LPAD function pads a string on the left. Following is its syntax:
LTRIM
The LTRIM function removes leading characters, usually leading spaces, from a
string. Following is its syntax:
LTRIM(string[, trimchars])
Note in the last example that all ‘*’ and all ‘!’ characters are removed. That’s
because both of those characters are listed in the trimchars string.
MOD
The MOD function returns the remainder of one value divided by another. Following
is its syntax:
MOD(number, divisor)
MONTHS_BETWEEN
The MONTHS_BETWEEN function returns the number of months between two dates.
Following is its syntax:
MONTHS_BETWEEN(date_1, date_2)
If both date_1 and date_2 represent the same day of the month, or if they both
represent the last day of the month, then MONTHS_BETWEEN will return an integer.
Otherwise, the function will return a fractional value. Also, if date_1 is less than
date_2, the value returned will be negative.
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 980
980 Appendixes
MONTHS_BETWEEN(TO_DATE(‘29-Dec-1999’,’dd-mon-yyyy’),
TO_DATE(‘29-Dec-1988’,’dd-mon-yyyy’))
= 132
MONTHS_BETWEEN(TO_DATE(‘29-Dec-1999’,’dd-mon-yyyy’),
TO_DATE(‘24-Nov-1988’,’dd-mon-yyyy’))
= 133.16129
In the first example, both dates represent the 29th of the month, so the result is an
integer. In the second example, because the dates represent different days of the
month, a fractional value, based on 31 days in the month, is returned.
NEW_TIME
The NEW_TIME function converts a date/time value between time zones. Following
is its syntax:
Table B-3
Time Zone Identifiers
Identifier Time Zone
NEXT_DAY
The NEXT_DAY function takes a date, determines the next occurrence of a specified
day of the week from that date, and returns the result. Following is its syntax:
NEXT_DAY(date, weekday)
NEXT_DAY(TO_DATE(‘24-Nov-1999’,’dd-mon-yyyy’),’FRIDAY’)
= ‘26-Nov-1999’
NEXT_DAY(TO_DATE(‘24-Nov-1999’,’dd-mon-yyyy’),’WED’)
= ‘01-Dec-1999’
Note that in the second example, the date 24-Nov-1999 falls on a Wednesday.
However, the NEXT_DAY function returns the date of the subsequent Wednesday,
which is 1-Dec-1999.
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 982
982 Appendixes
NLS_CHARSET_DECL_LEN
The NLS_CHARSET_DECL_LEN function, given a size in bytes, returns the width in
terms of characters for an NCHAR or NVARCHAR2 column. Following is its syntax:
NLS_CHARSET_DECL_LEN(bytecnt, csid)
NLS_CHARSET_DECL_LEN(200, 1) = 200
NLS_CHARSET_ID
The NLS_CHARSET_ID function returns the ID number corresponding to a specific
NLS character set name. Following is its syntax:
NLS_CHARSET_ID(charset_name)
NLS_CHARSET_ID(‘US7ASCII’) = 1
NLS_CHARSET_NAME
The NLS_CHARSET_NAME function does the reverse of NLS_CHARSET_ID.
It returns the character set name corresponding to a specified ID number.
Following is its syntax:
NLS_CHARSET_NAME(charset_id)
NLS_CHARSET_NAME(1) = ‘US7ASCII’
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 983
NLS_INITCAP
The NLS_INITCAP function works like INITCAP, but it is designed for use with
national character set strings. Following is its syntax:
NLS_INITCAP(string[, ‘NLS_SORT=sort_seq_name’])
NLS_INITCAP(‘chocolate’,’NLS_SORT=XSPANISH) = ‘Chocolate’
NLS_LOWER
The NLS_LOWER function works like LOWER, but it is designed for use with national
character set strings. Following is its syntax:
NLS_LOWER(string[, ‘NLS_SORT=sort_seq_name’])
NLS_LOWER(‘CHOCOLATE’,’NLS_SORT=XSPANISH’) = ‘chocolate’
NLS_UPPER
The NLS_UPPER function works like UPPER, but it is designed for use with national
character set strings. Following is its syntax:
NLS_UPPER(string[, ‘NLS_SORT=sort_seq_name’])
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 984
984 Appendixes
NLS_LOWER(‘chocolate’,’NLS_SORT=XSPANISH’) = ‘CHOCOLATE’
NLSSORT
The NLSSORT function returns the string of bytes used to represent a value when
that value is being sorted using a linguistic sort sequence. Following is its syntax:
NLSSORT(string[, ‘NLS_SORT=sort_seq_name’])
NLSSORT (‘chocolate’,’NLS_SORT=XSPANISH’)
= ‘205A1E5A4B146E2800020202020202020200’
NLSSORT (‘chocolate’,’NLS_SORT=XSPANISH’)
= ‘43686F636F6C61746500’
NVL
The NVL function takes two values as an argument. The second value represents an
alternative to the first and is returned if the first value is null. Following is its
syntax:
NVL(value, alternative)
The following example shows how NVL may be used to supply an alternate value for
a database column in the event that the column contains a null value:
In this example, if an employee has a name, then that name will be returned by the
NVL function. However, if an employee’s name is null, then the text ‘Name
Missing!’ will be returned instead.
POWER
The POWER function returns the result of raising a number to a specified power.
Following is its syntax:
POWER(number, power)
RAWTOHEX
The RAWTOHEX function converts a raw value to a string of hexadecimal values.
Following is its syntax:
RAWTOHEX(raw_value)
The examples shown in Listing B-3 illustrate the use of the RAWTOHEX function:
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 986
986 Appendixes
Table created.
1 row created.
RAWTOHEX(Y)
--------------------
414243
REPLACE
The REPLACE function searches a string for a specified substring and replaces that
substring with another. Following is its syntax:
REPLACE(string, substring[,replace_string])
ROUND(date[, fmt])
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 987
The following examples illustrate the use of the ROUND function for dates:
Note that the rounding may result in date changes. In the first example, because
8:00 PM is actually closer to the next day, the rounded result is the 25th, not the 24th.
ROUND(value, places)
The following examples illustrate the use of the ROUND function for numbers:
ROWIDTOCHAR
The ROWIDTOCHAR function converts a ROWID value to a character string.
Following is its syntax:
CHARTOROWID(rowid)
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 988
988 Appendixes
ROWIDTOCHAR(ROWID)
------------------
AAAADCAABAAAAVUAAA
RPAD
The RPAD function pads a string on the right. Following is its syntax:
RTRIM
The RTRIM function removes trailing characters, usually leading spaces, from a
string. Following is its syntax:
RTRIM(string[, trimchars])
RTRIM(‘Jeff*****’,’*’) = ‘Jeff’
RTRIM(‘Jeff*!*!*’,’*!’) = ‘Jeff’
Note in the last example that all ‘*’ and all ‘!’ characters were removed because
both of those characters are listed in the trimchars string.
SIGN
The SIGN function returns an integer representation of a number’s sign. The
following values are returned: –1, the number is negative; 0, the number is
zero; or 1, the number is positive. Following is its syntax:
SIGN(value)
value The input value, for which you want to know the sign
SIGN(-100) = -1
SIGN(0) = 0
SIGN(100) = 1
You can use the SIGN function together with DECODE to specify different
expressions to be evaluated depending on whether a value is positive,
negative, or zero.
SIN
The SIN function returns the sine of an angle. Following is its syntax:
SIN(angle)
angle The angle for which you want to know the sine. This
must be expressed in radians. See the description of
the COS function for information in converting from
degrees to radians.
SIN(90*3.1415926/180) = 1
SIN(0) = 0
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 990
990 Appendixes
SOUNDEX
The SOUNDEX function returns a phonetic representation of a string according to
the following rules, which are performed in the following order:
SOUNDEX(string)
SOUNDEX(‘Gennick’) = 520
SOUNDEX(‘Genik’) = 520
SOUNDEX(‘Genyk’) = 520
SQRT
The SQRT function returns the square root of a number. A square root multiplied by
itself yields the original number. Following is its syntax:
SQRT(number)
number The number for which you want to know the square root.
This cannot be a negative value.
SUBSTR
The SUBSTR function returns a specified portion of a string. Following is its syntax:
SUBSTR(‘JennyJeffJonathan’,6,4) = ‘Jeff’
SUBSTR(‘JennyJeffJonathan’,-12,4) = ‘Jeff’
SUBSTR(‘JennyJeffJonathan’,-8) = ‘Jonathan’
SUBSTRB
The SUBSTRB function returns a portion of a string. The SUBSTRB function is
almost identical to SUBSTR, but the start and length arguments refer to bytes,
not characters. This difference is apparent only when a multibyte character set
is being used. Following is its syntax:
SUBSTRB(‘JennyJeffJonathan’,6,4) = ‘Jeff’
SUBSTRB(‘JennyJeffJonathan’,-12,4) = ‘Jeff’
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 992
992 Appendixes
SUBSTRB(‘JennyJeffJonathan’,-8) = ‘Jonathan’
SYS_CONTEXT
The SYS_CONTEXT function returns the value of an attribute in an application
context namespace. Following is its syntax:
SYS_CONTEXT(namespace, attribute)
Table B-4
Predefined Attributes in the USERENV Namespace
Attribute Name Description
The examples shown in Listing B-4 illustrate the use of the SYS_CONTEXT function:
SYS_CONTEXT(‘USERENV’,’SESSION_USER’)
---------------------------------------------------
SEAPARK
SYS_CONTEXT(‘USERENV’,’NLS_TERRITORY’)
---------------------------------------------------
AMERICA
Your results, of course, will vary from these depending on the settings for your
system and your logon username.
SYS_GUID
The SYS_GUID function returns a 16-byte globally unique identifier. The return
type is RAW. On most platforms, the value consists of a host identifier, a process
or thread identifier, and a sequence number. Following is its syntax:
SYS_GUID()
SYS_GUID()
--------------------------------
A3B7D624A27111D38137005004AD2DB6
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 994
994 Appendixes
SYSDATE
The SYSDATE function returns the current date and time resolved down to the
second. Following is its syntax:
SYSDATE
SYSDATE
---------------------
24-Nov-1999 06:45:00 pm
Your results, of course, will vary from these depending on when you invoke
the function.
TAN
The TAN function returns the tangent of an angle. Following is its syntax:
TAN(angle)
TAN(0) = 0
TAN(225*3.1415926535/180) = 1
TANH
The TANH function returns the hyperbolic tangent of an angle. Following is its
syntax:
TANH(angle)
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 995
TANH(0) = 0
TANH(225*3.1415926535/180) = .99922389
The examples shown in Listing B-5 illustrate the use of the TO_CHAR function for dates:
TO_CHAR(SYS
-----------
24-Nov-1999
SQL> SELECT
2 TO_CHAR(SYSDATE,’Month’,’NLS_DATE_LANGUAGE=Spanish’)
3 FROM dual;
TO_CHAR(SY
----------
Noviembre
5
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 996
996 Appendixes
The following examples illustrate the use of the TO_CHAR function for numbers:
TO_CHAR(123.45) = ‘123.45’
TO_CHAR(123456.78,’$999,999.99’) = ‘$123,456.78’
TO_CHAR(123456.78,’L999G999D99’,
‘NLS_NUMERIC_CHARACTERS=’’,.’’ NLS_CURRENCY=’’!’’’)
= ‘!123.456,78’
Note that for the NLS parameters to have any effect, you must use L, G, and D for
the currency symbol, group separator, and decimal, respectively.
TO_DATE
The TO_DATE function converts a character-string value to a date. Following is
its syntax:
language The language to use. This can affect the spelling used for days
of the week and months. This is an optional argument.
The following examples illustrate the use of the TO_DATE function. The first
TO_DATE function interprets the date as 8-Nov-1915:
TO_DATE(‘11/08/1915’,’MM/DD/YY’)
TO_DATE(‘11/08/1915’,’DD/MM/YY’)
TO_DATE(‘11-Nov-1915,’DD-MON-YYYY’)
TO_LOB
The TO_LOB function converts a value of type LONG or LONG RAW to one of the
following: CLOB, BLOB, or NCLOB. You can use this function only in the subquery of
an INSERT statement when selecting LONG values to be inserted into LOB columns.
Following is its syntax:
TO_LOB(long_value)
long_value A value of type LONG or LONG RAW. The LONG type values
are converted to CLOB or NCLOB, depending on the type of the destination
column. The LONG RAW type values are converted to BLOBs.
TO_MULTI_BYTE
The TO_MULTI_BYTE function is useful only where multibyte character sets are
being used, and it converts all the singlebyte characters in a string to their
corresponding multibyte characters. Any singlebyte characters that have no
corresponding multibyte characters are left as they are. Following is its syntax:
TO_MULTI_BYTE(string)
998 Appendixes
multibyte_string := TO_SINGLE_BYTE(singlebyte_string);
TO_NUMBER
The TO_NUMBER function converts a character string to a number. Following is
its syntax:
The following examples illustrate the use of the TO_NUMBER function. The first
example interprets the number as 123.45:
TO_NUMBER(‘123.45’)
TO_NUMBER(‘$123,456.78’, ‘$999,999.99’)
TO_SINGLE_BYTE
The TO_SINGLE_BYTE function is useful only where multibyte character sets
are being used, and it converts all the multibyte characters in a string to their
corresponding singlebyte equivalents. Any multibyte characters that have no
corresponding singlebyte characters are left as they are. Following is its syntax:
TO_SINGLE_BYTE(string)
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 999
singlebyte_string := TO_SINGLE_BYTE(multibyte_string);
TRANSLATE
The TRANSLATE function allows you to translate one set of characters into another.
Following is its syntax:
The TRANSLATE function scans the input string, finds characters that occur
in charset1, and replaces each of those with a corresponding character from
charset2.
TRANSLATE(‘this is a code’,
‘abcdefghijklmnopqrstuvwxyz’,
‘zyxwvutsrqponmlkjihgfedcba’) = ‘gsrh rh z xlwv’
TRANSLATE(‘abcde’,’abc’,’xyz’) = ‘xyzde’
TRANSLATE(‘abcde’,’abc’,’xy’) = ‘xyde’
Notice from the third example that any characters in charset1 that don’t have
corresponding values in charset2 are deleted.
TRANSLATE USING
The TRANSLATE USING function translates strings between the database character
set and the national character set. Following is its syntax:
1000 Appendixes
USING CHAR_CS Converts the string into the database character set.
In this case, the function returns a VARCHAR2 value.
USING NCHAR_CS Converts the string into the national character set. In
this case, the function returns an NVARCHAR2 value.
The following examples illustrate the use of the TRANSLATE USING function:
TRIM
The TRIM function removes leading and/or trailing characters from a string.
Following is its syntax:
TRUNC(date[, fmt])
fmt One of the date format elements listed in Table B-2. The date will
be truncated to the element specified by this format. If you omit
this argument, the date will be truncated to the nearest day.
The following examples illustrate the use of the TRUNC function for dates:
Note Compare the results of the TRUNC function with those shown for the ROUND function.
TRUNC(number[,decimals])
The following examples illustrate the use of the TRUNC function for numbers:
TRUNC(89.985, 2) = 89.98
TRUNC(89.985) = 89
TRUNC(89.985,-1) = 80
Notice that a negative value for the decimals argument results in digits to the left
of the decimal point being replaced by zeros.
UID
The UID function returns an integer that is unique to the current database user.
Following is its syntax:
UID
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 1002
1002 Appendixes
The UID function takes no parameters. The following example illustrates the use of
the UID function:
UID
---------
5
This value comes from the USER# column in the V$SESSION view.
UPPER
The UPPER function returns the uppercase version of a string. Following is its syntax:
UPPER(string)
USER
The USER function returns the name of the current user. Following is its syntax:
USER
USER
------------------------------
SYSTEM
When invoked from within a stored function or procedure, this function will return
the name of the function or the procedure’s owner.
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 1003
USERENV
The USERENV function returns a variety of information about the current user.
Following is its syntax:
USERENV(option)
Table B-5
USERENV Options
Option Description
USERENV(‘LANGUAGE’)
--------------------------------------------
AMERICAN_AMERICA.WE8ISO8859P1
USERENV(‘INSTANCE’)
-------------------
1
4623-6 AppB.f.qc 1/28/00 12:36 PM Page 1004
1004 Appendixes
VSIZE
The VSIZE function returns the size, in bytes, of the internal representation of a
value. Following is its syntax:
VSIZE(value)
VSIZE(SYSDATE)
--------------
8
VSIZE(DUMMY)
------------
1
The first example tells you that the internal representation of a date value
consumes 8 bytes. The second example tells you that the internal representation of
the DUAL table’s DUMMY column uses only 1 byte.
✦ ✦ ✦