Skip to content

Built-in Functions in SQL

Numberic Functions

FunctionInput ArgumentValue Returned
ABS ( m )m = valueAbsolute value of m
MOD ( m, n )m = value, n = divisorRemainder of m divided by n
POWER ( m, n )m = value, n = exponentm raised to the nth power
ROUND ( m [, n ] )m = value, n = number of decimal places, default 0m rounded to the nth decimal place
TRUNC ( m [, n ] )m = value, n = number of decimal places, default 0m truncated to the nth decimal place
SIN ( n )n = angle expressed in radianssine (n)
COS ( n )n = angle expressed in radianscosine (n)
TAN ( n )n = angle expressed in radianstan (n)
ASIN ( n )n is in the range -1 to +1arc sine of n in the range -π/2 to +π/2
ACOS ( n )n is in the range -1 to +1arc cosine of n in the range 0 to π
ATAN ( n )n is unboundedarc tangent of n in the range -π/2 to + π/2
SINH ( n )n = valuehyperbolic sine of n
COSH ( n )n = valuehyperbolic cosine of n
TANH ( n )n = valuehyperbolic tangent of n
SQRT ( n )n = valuepositive square root of n
EXP ( n )n = valuee raised to the power n
LN ( n )n > 0natural logarithm of n
LOG ( n2, n1 )base n2 any positive value other than 0 or 1, n1 any positive valuelogarithm of n1, base n2
CEIL ( n )n = valuesmallest integer greater than or equal to n
FLOOR ( n )n = valuegreatest integer smaller than or equal to n
SIGN ( n )n = value-1 if n < 0, 0 if n = 0, and 1 if n > 0

Here are some examples of the use of some of these numeric functions:

select round (83.28749, 2) from dual;
select sqrt (3.67) from dual;
select power (2.512, 5) from dual;

String Functions

FunctionInput ArgumentValue Returned
INITCAP ( s )s = character stringFirst letter of each word is changed to uppercase and all other letters are in lower case.
LOWER ( s )s = character stringAll letters are changed to lowercase.
UPPER ( s )s = character stringAll letters are changed to uppercase.
CONCAT ( s1, s2 )s1 and s2 are character stringsConcatenation of s1 and s2. Equivalent to s1 || s2
LPAD ( s1, n [, s2] )s1 and s2 are character strings and n is an integer valueReturns s1 right justified and padded left with n characters from s2; s2 defaults to space.
RPAD ( s1, n [, s2] )s1 and s2 are character strings and n is an integer valueReturns s1 left justified and padded right with n characters from s2; s2 defaults to space.
LTRIM ( s [, set ] )s is a character string and set is a set of charactersReturns s with characters removed up to the first character not in set; defaults to space
RTRIM ( s [, set ] )s is a character string and set is a set of charactersReturns s with final characters removed after the last character not in set; defaults to space
REPLACE ( s, search_s [, replace_s ] )s = character string, search_s = target string, replace_s = replacement stringReturns s with every occurrence of search_s in s replaced by replace_s; default removes search_s
SUBSTR ( s, m [, n ] )s = character string, m = beginning position, n = number of charactersReturns a substring from s, beginning in position m and n characters long; default returns to end of s.
LENGTH ( s )s = character stringReturns the number of characters in s.
INSTR ( s1, s2 [, m [, n ] ] )s1 and s2 are character strings, m = beginning position, n = occurrence of s2 in s1Returns the position of the nth occurrence of s2 in s1, beginning at position m, both m and n default to 1.

Here are some examples of the use of String functions:

 select concat ('sagar', ' Sidana') as "NAME" from dual;
 select 'Alan' || 'Turing' as "NAME" from dual;
 select initcap ("now is the time for all good men to come to the aid of the
party") as "SLOGAN" from dual;
select substr ('Alan Turing', 1, 4) as "FIRST" from dual;

String / Number Conversion Functions

FunctionInput ArgumentValue Returned
NANVL ( n2, n1 )n1, n2 = valueif (n2 = NaN) returns n1 else returns n2
TO_CHAR ( m [, fmt ] )m = numeric value, fmt = formatNumber m converted to character string as specified by the format
TO_NUMBER ( s [, fmt ] )s = character string, fmt = formatCharacter string s converted to a number as specified by the format

 


Formats for TO_CHAR Function

SymbolExplanation
9Each 9 represents one digit in the result
0Represents a leading zero to be displayed
$Floating dollar sign printed to the left of number
LAny local floating currency symbol
.Prints the decimal point
,Prints the comma to represent thousands

Group Functions

FunctionInput ArgumentValue Returned
AVG ( [ DISTINCT | ALL ] col )col = column nameThe average value of that column
COUNT ( * )noneNumber of rows returned including duplicates and NULLs
COUNT ( [ DISTINCT | ALL ] col )col = column nameNumber of rows where the value of the column is not NULL
MAX ( [ DISTINCT | ALL ] col )col = column nameMaximum value in the column
MIN ( [ DISTINCT | ALL ] col )col = column nameMinimum value in the column
SUM ( [ DISTINCT | ALL ] col )col = column nameSum of the values in the column
CORR ( e1, e2 )e1 and e2 are column namesCorrelation coefficient between the two columns after eliminating nulls
MEDIAN ( col )col = column nameMiddle value in the sorted column, interpolating if necessary
STDDEV ( [ DISTINCT | ALL ] col )col = column nameStandard deviation of the column ignoring NULL values
VARIANCE ( [ DISTINCT | ALL ] col )col = column nameVariance of the column ignoring NULL values

 


Date and Time Functions

FunctionInput ArgumentValue Returned
ADD_MONTHS ( d, n )d = date, n = number of monthsDate d plus n months
LAST_DAY ( d )d = dateDate of the last day of the month containing d
MONTHS_BETWEEN ( d, e )d and e are datesNumber of months by which e precedes d
NEW_TIME ( d, a, b )d = date, a = time zone (char), b = time zone (char)The date and time in time zone b when date d is for time zone a
NEXT_DAY ( d, day )d = date, day = day of the weekDate of the first day of the week after d
SYSDATEnoneCurrent date and time
GREATEST ( d1, d2, …, dn )d1 … dn = list of datesLatest of the given dates
LEAST ( d1, d2, …, dn )d1 … dn = list of datesEarliest of the given dates

Date Conversion Functions

FunctionInput ArgumentValue Returned
TO_CHAR ( d [, fmt ] )d = date value, fmt = format for stringThe date d converted to a string in the given format
TO_DATE ( s [, fmt ] )s = character string, fmt = format for dateString s converted to a date value
ROUND ( d [, fmt ] )d = date value, fmt = format for stringDate d rounded as specified by the format
TRUNC ( d [, fmt ] )d = date value, fmt = format for stringDate d truncated as specified by the format

 


Date Formats

Format CodeDescriptionRange of Values
DDDay of the month1 – 31
DYName of the day in 3 uppercase lettersSUN, …, SAT
DAYComplete name of the day in uppercase, padded to 9 charactersSUNDAY, …, SATURDAY
MMNumber of the month1 – 12
MONName of the month in 3 uppercase lettersJAN, …, DEC
MONTHName of the month in uppercase padded to a length of 9 charactersJANUARY, …, DECEMBER
RMRoman numeral for the monthI, …, XII
YY or YYYYTwo or four digit year71 or 1971
HH:MI:SSHours : Minutes : Seconds10:28:53
HH 12 or HH 24Hour displayed in 12 or 24 hour format1 – 12 or 1 – 24
MIMinutes of the hour0 – 59
SSSeconds of the minute0 – 59
AM or PMMeridian indicatorAM or PM
SPA suffix that forces the number to be spelled out.e.g. TWO THOUSAND NINE
THA suffix meaning that the ordinal number is to be addede.g. 1st, 2nd, 3rd, …
FMPrefix to DAY or MONTH or YEAR to suppress paddinge.g. MONDAY with no extra spaces at the end

Here are some examples of the use of the Date functions:

select to_char ( sysdate, 'MON DD, YYYY' ) from dual;
 select to_char ( sysdate, 'HH12:MI:SS AM' ) from dual;
 select greatest ( to_date ( 'JAN 19, 2000', 'MON DD, YYYY' ),
                  to_date ( 'SEP 27, 1999', 'MON DD, YYYY' ),
                          to_date ( '13-Mar-2009', 'DD-Mon-YYYY' ) )
from dual;