English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PostgreSQL Common Functions

PostgreSQL built-in functions, also known as aggregate functions, are used to process string or numeric data.

The following is a list of all general PostgreSQL built-in functions:

  • COUNT Function: Used to calculate the number of rows in a database table.

  • MAX Function: Used to query the maximum value in a specific column.

  • MIN Function: Used to query the minimum value in a specific column.

  • AVG Function: Used to calculate the average of a specific column.

  • SUM Function: Used to calculate the sum of all values in a numeric column.

  • ARRAY Functions: Used to add input values (including null) to an array.

  • Numeric Functions: List all the functions required for operands in SQL.

  • String Functions: List all the functions required for operations in SQL.

Mathematical Functions

The following is a list of mathematical functions provided by PostgreSQL. It should be noted that many of these functions exist in multiple forms, the difference being the type of parameters. Unless otherwise specified, any specific form of a function returns the same data type as its parameters.

FunctionReturn TypeDescriptionexampleresult
absolute value of x
absolute valueabs(-17.4)17.4
cbrt(double
cube rootcbrt(27.0)3
ceil(double/numeric)
the smallest integer not less than the argumentceil(-42.8)-42
degrees(double)
convert radians to degreesdegrees(0.5)28.6478897565412
exp(double/numeric)
natural exponentiationexp(1.0)2.71828182845905
floor(double/numeric)
the largest integer not greater than the argumentfloor(-42.8)-43
ln(double/numeric)
natural logarithmln(2.0)0.693147180559945
log(double/numeric)
10logarithm to the baselog(100.0)2
log(b numeric,x numeric)numericlogarithm with specified baselog(2.0, 64.0)6.0000000000
mod(y, x)
get the remaindermod(9,4)1
pi()double"π" constantpi()3.14159265358979
power(a double, b double)doublecalculate the power of a to the bpower(9.0, 3.0)729
power(a numeric, b numeric)numericcalculate the power of a to the bpower(9.0, 3.0)729
radians(double)doubleconvert an angle to radiansradians(45.0)0.785398163397448
random()double0.0 to1to generate a random number between 0.0 andrandom()
round(double/numeric)
round to the nearest integerround(42.4)42
round(v numeric, s int)numericround to s decimal placesround(42.438,2)42.44
sign(double/numeric)
sign of the parameter(-1,0,+1)sign(-8.4)-1
sqrt(double/numeric)
square rootsqrt(2.0)1.4142135623731
trunc(double/numeric)
truncate (towards zero)trunc(42.8)42
trunc(v numeric, s int)numerictruncate to s decimal placestrunc(42.438,2)42.43

list of trigonometric functions

FunctionDescription
acos(x)arccosine
asin(x)arcsine
atan(x)arctangent
atan2(x, y)tangent y/inverse of x
cos(x)cosine
cot(x)cotangent
sin(x)sine
tan(x)tangent

String Functions and Operators

Below is a list of string operators provided by PostgreSQL:

FunctionReturn TypeDescriptionexampleresult
string || stringtextstring concatenation'Post' || 'greSQL'PostgreSQL
bit_length(string)intnumber of binary bits in a stringbit_length('jose')32
char_length(string)intnumber of characters in a stringchar_length('jose')4
convert(string using conversion_name)textchange encoding using the specified conversion name.convert('PostgreSQL' using iso_8859_1_to_utf8)'PostgreSQL'
lower(string)textconvert a string to lowercaselower('TOM')tom
octet_length(string)intnumber of bytes in a stringoctet_length('jose')4
overlay(string placing string from int [for int])textreplacing a substringoverlay('Txxxxas' placing 'hom' from 2 for 4)Thomas
position(substring in string)intthe position of the specified substringposition('om' in 'Thomas')3
substring(string [from int] [for int])textextracting a substringsubstring('Thomas' from 2 for 3)hom
substring(string from pattern)textExtract the substring that matches the POSIX regular expressionsubstring('Thomas' from '…$')mas
substring(string from pattern for escape)textExtract the substring that matches the SQL regular expressionsubstring('Thomas' from '%#"o_a#"_' for '#')oma
trim([leading丨trailing 丨 both] [characters] from string)textFrom the beginning of the string string/End/Both sides/ Remove the longest substring that only contains characters (default is a blank)trim(both 'x' from 'xTomxx')Tom
upper(string)textConvert the string to uppercase.upper('tom')TOM
ascii(text)intASCII code of the first character of the parameterascii('x')120
btrim(string text [, characters text])textRemove the longest substring at the beginning and end of the string that only contains characters in characters (default is blank).btrim('xyxtrimyyx','xy')trim
chr(int)textCharacter represented by ASCII codechr(65)A
convert(string text, [src_encoding name,] dest_encoding name)textConvert the string to dest_encodingconvert( 'text_in_utf8', 'UTF8', 'LATIN1)In ISO 8859-1Encoding representation of text_in_utf8
initcap(text)textConvert the first letter of each word to uppercase, while keeping the others in lowercase. A word is a series of alphanumeric characters separated by non-alphanumeric characters.initcap('hi thomas')Hi Thomas
length(string text)intThe number of characters in the stringlength('jose')4
lpad(string text, length int [, fill text])textBy filling with characters fill (default is blank), fill the string to the length length. If the string is already longer than length, it will be truncated (on the right).lpad('hi', 5, 'xy')xyxhi
ltrim(string text [, characters text])textRemove the longest word containing only characters (default is a space) from the beginning of the string string.ltrim('zzzytrim','xyz')trim
md5(string text)textCalculate the MD of the given string5Hash, return the result in hexadecimal.md5('abc')
repeat(string text, number int)textRepeat string number times.repeat('Pg', 4)PgPgPgPg
replace(string text, from text, to text)textReplace all occurrences of substring from in string with substring to.replace('abcdefabcdef', 'cd', 'XX')abXXefabXXef
rpad(string text, length int [, fill text])textFill the string to the length length by padding characters fill (default is a space). If the string is already longer than length, it will be truncated.rpad('hi', 5, 'xy')hixyx
rtrim(string text [, character text])textRemove the longest word containing only character(s) (default is a space) from the end of the string string.rtrim('trimxxxx','x')trim
split_part(string text, delimiter text, field int)textReturn the generated substring of the fieldth by splitting string with delimiter1 Base).split_part('abc~@~def~@~ghi', '~@~', 2)def
strpos(string, substring)textThe position of the declared substring.strpos('high','ig')2
substr(string, from [, count])textExtract a substring.substr('alphabet', 3, 2)ph
to_ascii(text [, encoding])textConvert text from other encoding to ASCII.to_ascii('Karel')Karel
to_hex(number int/bigint)textConvert number to its corresponding hexadecimal representation.to_hex(9223372036854775807)7fffffffffffffff
translate(string text, from text, to text)textConvert any character in string that matches the character in from to the corresponding character in to.translate('12345', '14', 'ax')a23x5

Type Conversion Related Functions

FunctionReturn TypeDescriptionExample
to_char(timestamp, text)textConvert timestamp to stringto_char(current_timestamp, 'HH12:MI:SS')
to_char(interval, text)textConvert time interval to stringto_char(interval '15h 2m 12s', 'HH24:MI:SS')
to_char(int, text)textConvert integer to stringto_char(125, '999)
to_char(double precision, text)textConvert double precision to stringto_char(125.8::real, '999D9)
to_char(numeric, text)textConvert number to stringto_char(-125.8, '999D99S')
to_date(text, text)dateConvert string to dateto_date('05 Dec 2000', 'DD Mon YYYY')
to_number(text, text)numericConvert string to numberto_number('12,454.8-', '99G999D9S')
to_timestamp(text, text)timestampConvert to specified time format time zone convert string to time stampto_timestamp('05 Dec 2000', 'DD Mon YYYY')
to_timestamp(double precision)timestampConvert UNIX epoch to timestampto_timestamp(1284352323)

Reference article: https://blog.csdn.net/sun5769675/article/details/50628979