Single-Row Functions

These functions return a value for each record for a queried table or a view. There are 7 main categories listed below.

  • Numeric
  • Character
    • returning number
    • returning character
  • Date / Date time
  • NULL related
  • Comparison
  • Conversion
  • Hierarchical

In tables below, you can find the most often used functions. For the complete list please refer to Oracle documentation pages.

Numeric functions

Numeric functions work with numeric data only – that means, they require a numerical value and return number.

FunctionDescriptionUsage
ABSreturns an absolute value of a numberSELECT abs(-1) as abs_value FROM dual;

abs_value
----------------
1
CEILalways rounds up a given integer if it is not a whole number already.SELECT ceil(10.1) as ceil FROM dual;

ceil
----------------
11
FLOORalways rounds down a given interger if it is not a whole number alreadySELECT floor(10.9) as floor FROM dual;

floor
----------------
10
ROUNDaccepts two parameter. First parameter is a given integer and the second one defines the precision for rounding (if omitted, it is rounded to zero decimal places) or number of decimal numbers after the decimal points if you will. If the second parameter is negative the round mechanism is applied to the left side of the decimal point.SELECT round(10.92,1) as round FROM dual;

round
----------------
10.9

SELECT round(56.92,-1) as round FROM dual;

round
----------------
60

SELECT round(10.925,2) as round FROM dual;

round
----------------
10.93
TRUNC (number)returns a truncated integer to a specific set of decimal points if a given parameter is numeric (there is an equivalent of this function accepting date parameter .. see below for more details). If the second parameter is negative then the truncate mechanism is applied to the left side of the decimal point.SELECT trunc(10.92,1) as trunc FROM dual;

trunc
----------------
10.9

SELECT trunc(10.97,1) as trunc FROM dual;

trunc
----------------
10.9

SELECT trunc(59.123,-1) as trunc FROM dual;

trunc
----------------
50

Character

Character functions divide into two categories based on whether they return a character or numeric value.

Returning character values

Character functions returning character values return either VARCHAR2 (for input parameters of CHAR, VARCHAR2) or NVARCHAR2( for input parameters of NCHAR, NVARCHAR2).

FunctionDescriptionUsage
CHRreturn a character based on its binary representative (as a parameter)SELECT chr(65) as chr FROM dual

chr
----------------
A
CONCATgets two parameters and returns their concatenation (join them together)SELECT concat('Oracle ','World') as concatFROM dual

concat
----------------
Oracle World
INITCAPreturns a given string with the first letter capitalized for each word in the string. Words are determined by a space or non-alphanumeric character.SELECT initcap('oracle_WORLD example') as initcap FROM dual

initcap
----------------
Oracle_World Example
LOWERreturns a given string with all letters lowercase.
SELECT lower('ORACLE World') as lower FROM dual

lower
----------------
oracle world
LPADreturns a given string (parameter1) left-padded by number of characters (parameter2) given as a last (parameter3) parameter.SELECT lpad('Oracle World', 20, 'x') as lpad FROM dual

lpad
----------------
xxxxxxxxOracle World
LTRIMremoves from the left side all defined characters (if not defined, single blank is used) from a given string.SELECT ltrim('---Oracle World---','-') as ltrim FROM dual

ltrim
----------------
Oracle World---
REGEXP_REPLACEextends REPLACE function by using regular expressions. VERY HEAVY ON CPU!! Do not use in complex queries!SELECT regexp_replace('Ora1 _orld', '[[:digit:]]','cle') as regexp_replace FROM dual

regexp_replace
----------------
Oracle World
REGEXP_SUBSTRINGextends SUBSTR function by using regular expressions. VERY HEAVY ON CPU!! Do not use in complex queries!SELECT regexp_substr('http://www.oracle-world.com', '[a-z.-]+',12) as regexp_substr FROM dual

regexp_substr
----------------
oracle-world.com


REPLACEreturns a given string (parameter1) with a replaced subset of characters (parameter2) with a given replacement (parameter3). Unlike TRANSLATE, it can replace one character with more charactersSELECT replace('Oz World', 'z','racle') as replace FROM dual

replace
----------------
Oracle World
RPADreturns a given string (parameter1) right-padded by number of characters (parameter2) given as a last (parameter3) parameter.SELECT rpad('Oracle World', 20, 'x') as rpad FROM dual

rpad
----------------
Oracle Worldxxxxxxxx
RTRIMremoves from the right side all defined characters (if not defined, single blank is used) from a given string.SELECT rtrim('---Oracle World---','-') as rtrim FROM dual

rtrim
----------------
---Oracle World
SUBSTRreturns a subset of characters from a given string (parameter1) where the second parameter defines whether Oracle will start from the beginning (positive number; 0 is considered as 1) or backwards from the end (negative number) and from which position. The last (parameter3) defines how many characters should be extracted.SELECT substr('Oracle World',1,6) as substr FROM dual

substr
----------------
Oracle


SELECT substr('Oracle World',-1,5) as substr FROM dual

substr
----------------
World


SELECT substr('Oracle World',3,5) as substr FROM dual

substr
----------------
acle
TRANSLATEreturns a given string (parameter1) with a replaced subset of characters (parameter2) with a given replacement (parameter3). It can only replace one character by another.SELECT translate('Oxacle Woxld', 'x','r') as translate FROM dual

translate
----------------
Oracle World
TRIMreturns a trimmed string given as a first parameter. You can define whether you want to trim leading or trailing characters or both. If no specification is given, it trims leading and trailing spaces.SELECT trim(LEADING '-' FROM '---Oracle World---') as trim FROM dual

trim
----------------
Oracle World---


SELECT trim(TRAILING '-' FROM '---Oracle World---') as trim FROM dual

trim
----------------
---Oracle World


SELECT trim(BOTH '-' FROM '---Oracle World---') as trim FROM dual

trim
----------------
Oracle World


SELECT trim(' Oracle World ') as trim FROM dual

trim
----------------
Oracle World
UPPERreturns a given string with all letters uppercase.SELECT upper('ORACLE World') as upper FROM dual

upper
----------------
ORACLE WORLD

Returning numeric values

Character functions returning numeric values. They accept any character type as an input parameter.

FunctionDescriptionExample
ASCIIReturns a decimal representation of a given (single) character. Does not accept CLOBs.SELECT ascii('a') as ascii FROM dual

ascii
----------------
97
INSTRReturns a position in a given string based on given criteria. First parameter is a given string, second parameter is a string / character we are looking for in a string, third parameter is a starting position (if negative it is processed backwards) and the last parameter is n-th occurence of in the string of a lookup value.SELECT instr('oracle world', 'o', 1, 2) as instr FROM dual

instr
----------------
9
LENGTHreturns the length of a given string. Considers even leading, trailing spaces and other non-printable characters (carriage return, ...)SELECT length('Oracle World') as length FROM dual

length
----------------
12

SELECT length(' Oracle World ') as length FROM dual

length
----------------
18
REGEXP_COUNTreturns the number of occurrences based on a regular expression.SELECT regexp_count('Oracle World :: Developer''s Cheat Sheet', '[ ][A-P]', 1, 'c') as regexp_count FROM dual

regexp_count
----------------
2

REGEXP_INSTRextends INSTR function by using regular expressions. VERY HEAVY ON CPU!! Do not use in complex queries!SELECT regexp_instr('Oracle World :: Developer''s Cheat Sheet', '[ ]', 1, 3) as regexp_instr FROM dual

regexp_instr
----------------
16

Date / Date time functions

Returns a date, datetime or timestamp format.

FunctionDescriptionExample
ADD_MONTHSadd a specific number of months (negative number deducts) to a given dateSELECT add_months(DATE '2010-01-01', 3) as add_months FROM dual

add_months
----------------
04/01/2010

SELECT add_months(DATE '2010-01-31', 1) as add_months FROM dual

add_months
----------------
02/28/2010

SELECT add_months(DATE '2010-01-31', -3) as add_months FROM dual

add_months
----------------
10/31/2009
LAST_DAYreturns last day in a month for a given dateSELECT last_day(DATE '2010-01-03') as last_day FROM dual

last_day
----------------
1/31/2010
MONTHS_BETWEENreturns number of months between two given datesSELECT months_between(DATE '2015-07-14', DATE '2010-01-03') as months_between FROM dual

months_between
----------------
66.35483
NEXT_DAYreturns the next date for a specified week day starting from a given date as a first parameter.SELECT next_day(DATE '2015-07-14','MONDAY') as next_day FROM dual

next_day
----------------
07/20/2015
ROUNDreturns a date rounded to the specified date-unit.SELECT round(DATE '2015-07-14','YEAR') as round FROM dual

round
----------------
01/01/2016

SELECT round(DATE '2015-03-10','MONTH') as round FROM dual

round
----------------
03/01/2015

SYSDATEreturns the current datetime from the database server.SELECT sysdate as act_date FROM dual

act_date
----------------
8/2/2018 6:24:48 PM
TRUNCreturns a truncated date to the specified unit. (always down)SELECT trunc(DATE '2015-03-10','MM') as trunc FROM dual

trunc
----------------
03/01/2015

SELECT trunc(DATE '2015-03-10','iW') as trunc FROM dual

trunc
----------------
03/09/2015

SELECT trunc(DATE '2015-11-29','YYYY') as trunc FROM dual

trunc
----------------
01/01/2015

NULL-related functions

These functions handle NULL values.

FunctionDescriptionExample
COALESCEreturns the first non-null value from a given list of values (min. 2 values required)SELECT coalesce(NULL, NULL, 'Oracle', NULL, 'World') as coalesce FROM dual

coalese
----------------
Oracle
NVLreturns the second given value if the first is NULL. If the first is a non-null value, it returns the first one.SELECT nvl(NULL, 'Oracle World') as nvl FROM dual

nvl
----------------
Oracle World


SELECT nvl('Oracle', 'World') as nvl FROM dual

nvl
----------------
Oracle
NVL2returns the second parameter if the given value is not null. If the given value is null, it returns the third parameter.SELECT nvl2(NULL, 'Oracle World', 'Developer''s Cheat Sheet') as nvl2 FROM dual

nvl2
----------------
Developer's Cheat Sheet


SELECT nvl2('someString', 'Oracle World', 'Developer''s Cheat Sheet') as nvl2 FROM dual

nvl2
----------------
Oracle World

Comparison functions

They determine the greatest or least value from a given set of values.

FunctionDescriptionExample
GREATESTreturns the greatest value of a given set of values.SELECT greatest(1,5,31,2,20,3,39,3) as greatest FROM dual

greatest
----------------
39
LEASTreturns the least value of a given set of values.SELECT least(1,5,31,2,20,3,39,3) as least FROM dual

least
----------------
1

Conversion functions

Conversion functions convert values from one data type to another.

FunctionDescriptionExample
HEXTORAWconverts hexadecimal values to a character values.SELECT UTL_RAW.CAST_TO_VARCHAR2(hextoraw('4F7261636520576F726C64')) as hextoraw FROM dual

hextoraw
----------------
Oracle World
NUMTODSINTERVALconverts a given parameter to a "DAY TO SECOND" interval.SELECT numtodsinterval(10, 'SECOND') as numtodsinterval FROM dual

numtodsinterval
----------------
+000000000 00:00:10.000000000
NUMTOYMINTERVALconverts a given parameter to a "YEAR TO MONTH" interval.SELECT numtoyminterval(31, 'MONTH') as numtoyminterval FROM dual

numtoyminterval
----------------
+000000002-07
RAWTOHEXconverts character values to a hexadecimal valueSELECT rawtohex('OracLe World') as rawtohex FROM dual

rawtohex
----------------
4F7261636520576F726C64
TO_CHARaccepts three data types: Character, Number, Date.

Character:
Converts a given set of character to a STRING

Number:
Converts a given number to a STRING (trims leading zeros). You can set a format for an output value

Date:
Converts a given date to a STRING specified by a given format.
SELECT to_char('L1001') as to_char FROM dual

to_char
----------------
L1001


SELECT to_char(0123456.789, '999,999.9') as to_char FROM dual

to_char
----------------
123,456.8


SELECT to_char(DATE '2018-03-28', 'DAY') as to_char FROM dual

to_char
----------------
WEDNESDAY
TO_DATEreturns a data type DATE / DATETIME from a given string and format.SELECT to_date('11/20/2010 18:34:38 PM', 'MM/DD/YYYY HH24:MI:SS') as to_date FROM dual

to_date
----------------
11/20/2010 06:34:38 PM


TO_NUMBERreturn a NUMBER data type from a given string. You can specify the format of a number by providing a second parameter.SELECT to_number('123456.789', '999999.999') as to_number FROM dual

to_number
----------------
123456.789

Hierarchical functions

Hierarchical functions provide a hierarchical path for a result data set.

FunctionDescriptionExample
SYS_CONNECT_BY_PATHreturns path from a root node to a leaf node. It can be applied in hierarchical queries only.SELECT level as id_row
, sys_connect_by_path(to_char(sysdate+level,'DD'), '/') as sys_connect_by_path
FROM dual
CONNECT BY level <= 3

ID_ROW SYS_CONNECT_BY_PATH
------------------------------------------------------
1 /29
2 /29/30
3 /29/30/31