PostgreSQL String Functions

This page provides you with 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 separatorCONCAT_WS(‘,’,’A’,’B’,’C’)‘A,B,C’
FORMATFormat arguments based on a format stringFORMAT(‘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’
LPADPad on the left a a string with a character to a certain lengthLPAD(‘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 stringPOSTION(‘B’ in ‘A B C’)3
REGEXP_MATCHESMatch a POSIX regular expression against a string and returns the matching substringsSELECT REGEXP_MATCHES(‘ABC’, ‘^(A)(..)$’, ‘g’);{A,BC}
REGEXP_REPLACEReplace substrings that match a POSIX regular expression by a new substringREGEXP_REPLACE(‘John Doe’,'(.*) (.*)’,’\2, \1′);‘Doe, John’
REPEATRepeat string the specified number of timesREPEAT(‘*’, 5)‘*****’
REPLACEReplace all occurrences in a string of substring from with substring toREPLACE(‘ABC’,’B’,’A’)‘AAC’
REVERSEReturn reversed string.REVERSE(‘ABC’)‘CBA’
RIGHTReturn last n characters in the string. When n is negative, return all but first |n| characters.RIGHT(‘ABC’, 2)‘BC’
RPADPad on the right of a string with a character to a certain lengthRPAD(‘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 longest string that contains specified characters from the left, right or both of the input stringTRIM(‘ ABC  ‘)‘ABC’
UPPERConvert a string to uppercaseUPPER(‘hI tHERE’)‘HI THERE’