PostgreSQL String Functions

This page provides the most commonly used PostgreSQL string functions that allow you to manipulate string data effectively.

FunctionDescriptionExampleResult
ASCIIReturn the ASCII code value of a character or Unicode code point of a UTF8 characterASCII(‘A’)65
CHRConvert an ASCII code to a character or a Unicode code point to a UTF8 characterCHR(65)‘A’
CONCATConcatenate two or more strings into oneCONCAT(‘A’,’B’,’C’)‘ABC’
CONCAT_WSConcatenate strings with a specified separator.CONCAT_WS(‘,’,’A’,’B’,’C’)‘A,B,C’
FORMATFormat a string based on a templateFORMAT(‘Hello %s’,’PostgreSQL’)‘Hello PostgreSQL’
INITCAPConvert words in a string to title caseINITCAP(‘hI tHERE’)Hi There
LEFTReturn the first n character in a stringLEFT(‘ABC’,1)‘A’
LENGTHReturn the number of characters in a stringLENGTH(‘ABC’)3
LOWERConvert a string to lowercaseLOWER(‘hI tHERE’)‘hi there’
LPADExtending a string to a length by padding specified characters on the leftLPAD(‘123′, 5, ’00’)‘00123’
LTRIMRemove the longest string that contains specified characters from the left of the input stringLTRIM(‘00123’)‘123’
MD5Return MD5 hash of a string in hexadecimalMD5(‘ABC’)
POSITIONReturn the location of a substring in a stringPOSITION(‘B’ in ‘A B C’)3
REGEXP_MATCHESReplace substrings that match a POSIX regular expression with a new substringSELECT REGEXP_MATCHES(‘ABC’, ‘^(A)(..)$’, ‘g’);{A,BC}
REGEXP_REPLACEReplace a substring using regular expressions.REGEXP_REPLACE(‘John Doe’,'(.*) (.*)’,’\2, \1′);‘Doe, John’
REPEATRepeat a string the specified number of times.REPEAT(‘*’, 5)‘*****’
REPLACEReplace a substring within a string with a new one.REPLACE(‘ABC’,’B’,’A’)‘AAC’
REVERSEReplace a substring within a string with a new oneREVERSE(‘ABC’)‘CBA’
RIGHTReturn the last n characters in the string. When n is negative, return all but the first |n| characters.RIGHT(‘ABC’, 2)‘BC’
RPADExtend a string to a length by appending specified characters.RPAD(‘ABC’, 6, ‘xo’)‘ABCxox’
RTRIMRemove the longest string that contains specified characters from the right of the input stringRTRIM(‘abcxxzx’, ‘xyz’)‘abc’
SPLIT_PARTSplit a string on a specified delimiter and return nth substringSPLIT_PART(‘2017-12-31′,’-‘,2)’12’
SUBSTRINGExtract a substring from a stringSUBSTRING(‘ABC’,1,1)A’
TRIMRemove the leading and trailing characters from a string.TRIM(‘ ABC  ‘)‘ABC’
UPPERConvert a string to uppercaseUPPER(‘hI tHERE’)‘HI THERE’