You are on page 1of 5

Oracle SQL: Single-row functions.

http://bzupages.com/upload/books/Single-row functions. IN SQL Plus O...

Single-row functions. IN SQL Plus Oracle Queries


NULL

value functions.

Any arithmetic operation on a NULL value results in a NULL. The same model is true for most functions as well. Only functions
NVL NVL2 CONCAT DECODE DUMP and REPLACE

can return non-NULL value when called with a NULL argument.


NVL function

The NVL function takes two arguments,


NVL(x, y)

where both x and y are expressions. This function returns y, if x is NULL. If x is not NULL, it returns x.
NVL2

function

The function NVL2 is very similar to the NVL function. NVL2 takes three arguments
NVL2(x, y, z)

where x, y, and z are expressions. This function returns z if x is NULL, and y if x is not NULL. This function allows us to implement an IF...THEN...ELSE construct based on the nullity of data.

Character functions.
Description Example Returns the ASCII code of the first character of the ASCII(s) ASCII('test') = 116 string s. CHR(i) CHR(116) = 't' Returns the character with the ASCII code i. CONCAT(s1, s2) Returns string s2 appended to s1. CONCAT('Wel', 'come') = 'Welcome' Returns the string s with the first character of each word INITCAP(s) INITCAP('hello SIR') = 'Hello Sir' in uppercase and all others in lowercase. Returns the numeric character position in s where the n-th occurrence of sub is found. The search begins at the position pos in s. If the substring not found, it INSTR(s, sub[, pos[, n]]) INSTR('test', 't', 2) = 4 returns 0. If the pos is negative, the search is performed backwards (from right to left). The default values for pos and n are 1. LENGTH(s) LENGTH('Welcome Sir') = 11 Returns the number of characters in the string s.
LOWER(s) LPAD(s, i[, s1]) LTRIM(s, s1) RPAD(s, i[, s1])

Function

Returns the string s with all characters in lowercase. Returns the string s expanded in length to i characters, using s1 to fill in space as needed on the left side. Returns s without any leading character that appear in s1. If no s1 character are leading characters in s, then s is returned unchanged. Returns the string s expanded in length to i characters, using s1 to fill in space as needed on the right side. Returns s without any trailing character that appear in s1. If no s1 character are trailing characters in s, then s is returned unchanged. Returns s with all occurrences of substring s1 replaced with s2. By default s2 is NULL and all occurrences of s1 are removed. Returns the portion of the string s that is len characters long, beginning at position pos. If pos is negative, the position is counted backwards from the end of the

LOWER('Welcome Sir') = 'welcome sir' LPAD('test', 11, '*-') = '*-*-*-*test' LTRIM('welcome', 'slow') = 'elcome' LPAD('test', 11, '*-') = 'test*-*-*-*' RTRIM('Mississippi', 'pi') = 'Mississ' REPLACE('www.yahoo.com', 'yahoo', 'google') = 'www.google.com'

RTRIM(s, s1)

REPLACE(s, s1[, s2])

SUBSTR(s, pos[, len])

SUBSTR('welcome', 4) = 'come'

1 of 5

29-12-2011 10:38

Oracle SQL: Single-row functions.

http://bzupages.com/upload/books/Single-row functions. IN SQL Plus O...

SOUNDEX(s)

TRANSLATE(s, s1, s2)

TRIM([s1 kw FROM] s)

string. The function returns NULL if len is less or equal to zero. If len is skipped, it returns the remaining of s. Returns the soundex phonetic representation of the SOUNDEX('John') = J500 = string s. It's useful when we need to find words that SOUNDEX('Jon') sound like a given one. Returns the string s with all occurrences of characters in s1 replaced with the positionally corresponding TRANSLATE('alfabet', 'abscde', characters is s2.If s2 is shorter than s1, the unmatched 'BCDE') = 'BlfBCt' characters in s1 are removed. This function returns the string s with all s1 (leading, trailing, or both) occurrences of characters in s TRIM(BOTH '.' FROM 'etc ...') = removed. If present kw is one of the following LEADING, 'etc ' TRAILING, or BOTH (by default it's BOTH). The default value of s1 is a space character. Returns the string s with all characters in uppercase.
UPPER('Welcome Sir') = 'WELCOME SIR'

UPPER(s)

Numeric functions.
Description Returns the absolute value of the double d. ACOS(d) Returns the arc cosine of the value d expressed in radians (d should be between -1 and 1). ASIN(d) Returns the arc sine of the value d expressed in radians (d should be between -1 and 1). ATAN(d) Returns the arc tangent of the value d expressed in radians. ATAN2(d1, d2) Returns the arc tangent of the value d1/d2 expressed in radians.
ABS(d) BITAND(i1, i2) CEIL(d) COS(d) COSH(d) EXP(d) FLOOR(d) LN(d) LOG(b, d) MOD(i1, i2) POWER(d, p) ROUND(d, i) SIGN(d) SIN(d) SINH(d) SQRT(d) TAN(d) TANH(d) TRUNC(d, i)

Function

Returns the bitwise AND operation performed on two integer arguments i1 and i2. The result is also an integer. Returns the smallest integer that is greater or equal to d. Evaluates the cosine of d radians. Returns the hyperbolic cosine of d. Returns ed Returns the largest integer less or equal to d. Returns the natural logarithm of d. Returns logbd. Returns i1 modulo i2, or the reminder of i1 divided i2. Returns d to the pth power (dp). Returns d rounded to i digits of precision to the right of the decimal point. If i is negative, d is rounded to the left of the decimal point. Returns -1 if d is negative, 1 if d is positive, and 0 if d is zero. Evaluates the sine of d radians. Returns the hyperbolic sine of d. Returns the square root of d. Evaluates the tangent of d radians. Returns the hyperbolic tangent of d. Returns d truncated to i digits of precision to the right of the decimal point. If i is negative, d is truncated to the left of the decimal point.

Date functions.
Implicit Date-to-String conversion is based on the value of the NLS_DATE_FORMAT variable. To change its value use the ALTER SESSION command:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY HH24:MI:SS';

Function
ADD_MONTHS(dt, i)

CURRENT_DATE

Description Example Returns the date dt plus i months. If i is a decimal number. Oracle will automatically ADD_MONTHS(SYSDATE, 2) = '01-SEP-2003 12:00:03' convert it to an integer by truncating the decimal portion (it may also be negative). Returns the current date in the Gregorian calendar for the session's time zone. It requires CURRENT_DATE = '01-JUL-2003 12:00:35' no arguments.

2 of 5

29-12-2011 10:38

Oracle SQL: Single-row functions.

http://bzupages.com/upload/books/Single-row functions. IN SQL Plus O...

CURRENT_TIMESTAMP([p])

DBTIMEZONE

EXTRACT(c FROM dt)

Returns the current date and time in the session's time zone to p digits of the precision, p should be an integer 0 through 9 and defaults to 6. The return datatype is TIMESTAMP WITH TIME ZONE. (See also LOCALTIMESTAMP) Returns the database's time zone, as set by the latest CREATE DATABASE or ALTER DATABASE SET TIME_ZONE statement. The time zone is a string specifying the hours and minutes offset from UTC (Coordinated Universal Timezone, aka as GMT, or Greenwich Mean Time) or a time zone regional name. The valid time zone regional name can be found in the TZNAME column of the view V$TIMEZONE_NAMES Returns the component specified by c from the date/time or interval dt. The valid components are: TIMEZONE_HOUR YEAR MINUTE TIMEZONE_MINUTE MONTH SECOND DAY TIMEZONE_REGION HOUR TIMEZONE_ABBR

CURRENT_TIMESTAMP(4) = '01-JUL-03 12:00:51.8261 PM -04:00'

DBTIMEZONE =

'-07:00'

EXTRACT(MONTH FROM SYSDATE) = 7

Returns a timestamp with time zone for the timestamp ts using the time zone value tz. The FROM_TZ(LOCALTIMESTAMP, '+3:00') = FROM_TZ(ts, tz) string tz specifies the hours and minutes offset '01-JUL-03 12:01:57.679850 PM +03:00' from UTC or is a time zone region name. Returns the last day of the month for the date LAST_DAY('25-Feb-2004') = '29-FEB-2004 LAST_DAY(dt) 00:00:00' dt. Returns the current date and time in the session's time zone to p digits of the precision, p should be an integer 0 through 9 and LOCALTIMESTAMP(2) = '01-JUL-03 12:02:48.59 LOCALTIMESTAMP([p]) PM' defaults to 6. This function is similar to the CURRENT_TIMESTAMP, but the return datatype is TIMESTAMP. Returns the number of months that dt1 is later than dt2. A whole number is returned if dt1 MONTHS_BETWEEN(dt1, dt2) MONTHS_BETWEEN('01-JAN-2003', SYSDATE) = -6 and dt2 are the same day of the month or if both are th last day of the month. Returns the date in time zone tz2 for the date NEW_TIME(SYSDATE, 'EST', 'PDT') = NEW_TIME(dt, tz1, tz2) '01-JUL-2003 10:04:47' dt in time zone tz1. See time zone constants Returns the date that corresponds to the next day of week specified by the string s following the date dt. The time portion of the date is the NEXT_DAY(SYSDATE, 'Mon') = '07-JUL-2003 NEXT_DAY(dt, s) same as the time portion of dt. The string s 12:05:13' should be a text string containing the full or abbreviated day of the week in the session's language. Returns the date dt rounded to the granularity ROUND(SYSDATE, 'HH24') = '01-JUL-2003 ROUND(dt[, fmt]) specified in the date-format string fmt. See 11:00:00' date format codes. Returns the database's time zone as per the SESSIONTIMEZONE SESSIONTIMEZONE = '-04:00' last LATER SESSION statement. It takes no arguments.
SYS_EXTRACT_UTC(ts)

Returns the UTC (GMT) time zone from the timestamp ts.

CURRENT_TIMESTAMP = '01-JUL-03 11.48.19.618428 AM -04:00' SYS_EXTRACT_UTC(CURRENT_TIMESTAMP) = '01-JUL-03 03:47:05.917078 PM'

SYSDATE

SYSTIMESTAMP

Returns the current date/time, takes no SYSDATE = '01-JUL-2003 11:50:08' arguments. Takes no arguments and returns a TIMESTAMP WITH TIME ZONE for the SYSTIMESTAMP = '01-JUL-03 11:52:10.051237 current database date and time. The fractional AM -04:00' second is returned with six digits of precision.

3 of 5

29-12-2011 10:38

Oracle SQL: Single-row functions.

http://bzupages.com/upload/books/Single-row functions. IN SQL Plus O...

TRUNC(dt[, fmt]) TZ_OFFSET(tz)

Returns the date dt truncated to the TRUNC(SYSdate, 'DD') = '01-JUL-2003 granularity specified by the format string fmt. 00:00:00' Returns the numeric time zone offset for the TZ_OFFSET('US/Eastern') = '-04:00' textual time zone name tz.

Conversion functions.
Description Returns the ASCII equivalent of all characters in the string s. Takes a single argument bit_list - the comma-delimited list of bits, and returns the numeric BIN_TO_NUM(bit_list) representation of all the bit-field set bit_list. Converts the expression expr into datatype type. The expr can be an expression, subquery, or CAST(expr AS type) MULTISET clause. See the possible conversion table for standard datatypes. CHARTOROWID(s) Returns the string s as a ROWID datatype. COMPOSE(s) Returns the string s as a Unicode string in its fully normalized form, in the same characters set as s. CONVERT(s, dset[, sset]) Returns the string s converted from the soursecharacter set sset to the destination character set dset. DECOMPOSE(s) Returns the string s as a Unicode string after canonical decomposition in the same character set as s. HEXTORAW(x) Returns the hexadecimal string x converted to a RAW datatype. Converts the number x into an INTERVAL DAY TO SECOND datatype accordingly to the string st NUMTODSINTERVAL(x, st) denoting the units for x. Valid units are: 'DAY', 'HOUR', 'MINUTE', and 'SECOND'. Converts the number x into an INTERVAL YEAR TO MONTH datatype accordingly to the string st NUMTOYMINTERVAL(x, st) denoting the units for x. Valid units are: 'YEAR' and 'MONTH'. RAWTOHEX(rs) Returns the raw string rs converted to hexadecimal. ROWIDTOCHAR(x) Returns the string x (in the format of ROWID) converted from ROWID. Takes up to three arguments, where x is either a date or a number, fmt is a format string specifying the format that x will appear in, and nls specifies language or location formatting string. TO_CHAR(x[, fmt[, nls]]) If x is a date, fmt is a date format code. If x is a number, fmt is a numeric format code. Converts string s to DATE datatype accordingly the format string fmt. The fmt string uses the same TO_DATE(s[, fmt[, nls]]) date format code. The default is the value stored in the NLS_DATE_FORMAT session variable. TO_DSINTERVAL(s[, nls]) Returns the string s converted into an INTERVAL DAY TO SECOND datatype. Returns the numeric value represented by the string s. The format string fmt specifies the format the TO_NUMBER(s[, fmt[, nls]]) that s appears in. TO_YMINTERVAL(s) Returns the string s converted into an INTERVAL YEAR TO MONTH datatype. UNISTR(s) Returns the string s in Unicode in the database Unicode character set.
ASCIISTR(s)

Function

Other functions.
Function
BFILENAME(dir, file)

COALESCE(expr_list)

Description Takes two arguments, where dir is a string containing a directory name and file is a string containing a file name. Returns an empty BFILE locator. When used, the BFILE is instantiated. Neither dir nor file needs to exist at the time the function is called, but both must exist when the locator is used. Returns the first non-NULL value in the expr_list. If all expressions are NULL, then NULL is returned. Each expression in the expr_list should be the same type. The function compares the value of the expression x with values of the arguments v1, v2, etc. If the value of x is equivalent to v1, then r1 is returned; otherwise the additionalmatching expressions are compared. If no match is found and the default expression d is included, then the d is returned. If no match is found and there is no d argument, then NULL is returned. Example:
SELECT sid, serial#, username, DECODE(command, 0, 'None', 2, 'Instert', 3, 'Select', 6, 'Update', 7, 'Delete', 8, 'Drop', 'Other') cmd FROM v$session WHERE type <> 'BACKGROUND';

DECODE(x, v1, r1[, v2, r2 ...][, d])

DUMP(x[, fmt[, sb[, len]]])

Takes up to four arguments, where x is an expression, fmt is a format specification for octal (1008), decimal (1010), hexadecimal (1016),

4 of 5

29-12-2011 10:38

Oracle SQL: Single-row functions.

http://bzupages.com/upload/books/Single-row functions. IN SQL Plus O...

EMPTY_BLOB EMPTY_CLOB GREATEST(expr_list) LEAST(expr_list) NULLIF(x1, x2)

SYS_CONNECT_BY_PATH(col, chr)

or single character (1017) , sb is the starting byte offset within x, and len is the length in bytes to dump. The function returns a character string containing the datatype of x in numeric notation and the internal presentation of x. Returns an empty BLOB locator. This function is used to initialize a BLOB variable or BLOB column in a table. Returns an empty CLOB locator. This function is used to initialize a CLOB variable or CLOB column in a table. Returns the expression from the expr_list that sorts the highest in the datatype of the first expression. If the expression list contains a NULL, then a NULL is returned. Returns the expression from the expr_list that sorts the lowest in the datatype of the first expression. If the expression list contains a NULL, then a NULL is returned. Returns NULL if the expression x1 is equal to x2; otherwise returns x1. This function works only with hierarchical queries - queries that use CONNECT BY clause. It returns the path of column col, delimited by the character chr, from root to node for each row returned by the CONNECT BY condition. Example:
SELECT dep_id, dep_name, SYS_CONNECT_BY_PATH(dep_name, '-') 'Dep. Chain' FROM departments START WITH dep_id = 0 CONNECT BY PRIOR dep_id = sup_dep_id;

SYS_CONTEXT(ns, par[, len]) UID USER

USERENV(opt)

VSIZE(x)

Takes up to three arguments, where ns is a namespace, par is a parameter associated with the namespace ns, and len is the length of the return value in bytes (default 256). The built-in namespace USERENV contains information on the current session. See the table with parameters in the USERENV Returns the integer user ID for the current user. Returns a character string containing the username for the current user. Takes a single argument, where opt is one of the following options: ISDBA SESSIONID ENTRYID INSTANCE LANGUAGE LANG TERMINAL This function is deprecated in release Oracle9i. Returns the size in bytes of the internal representation of the expression x.

The source for the materials is OCA/CPA Introduction to Oracle9i SQL Study Guide by Chip Dawes and Biju Thomas www.BZUPAGES.com

5 of 5

29-12-2011 10:38

You might also like