PostgreSQL Cheat Sheet String Functions
PostgreSQL Cheat Sheet String Functions
PostgreSQL Cheat Sheet String Functions
CHEAT SHEET: STRING FUNCTIONS by postgresqlbackup.com with ♥
CONVERSION MODIFICATION
ASCII code of the first byte of the argument. Return the given string suitably quoted to be used as an String concatenation Split string on delimiter and return the given field (counting
identifier in an SQL statement string. Quotes are added from one)
ASCII ('x') = 120 only if necessary. Embedded quotes are properly doubled 'Postgre' || 'SQL' = 'PostgreSQL'
ASCII ('Ӓ') = 1234 'Value: ' || 42 = 'Value: 42' SPLIT_PART ('1,2,3', ',', 2) = '2'
QUOTE_IDENT ('ABC DEF') = '"ABC DEF"'
Convert string to ASCII from another encoding (only QUOTE_IDENT ('"ABC"') = '"""ABC"""' Remove the longest string containing only the characters (a Extract substring
supports conversion from LATIN1, LATIN2, LATIN9, and space by default) from the start/end/both ends of the string
WIN1250 encodings) Return the given string suitably quoted to be used as a SUBSTRING ('12345' from 2 for 3) = '234'
string literal in an SQL statement string. Embedded single BTRIM (' AB ') = 'AB' SUBSTR ('12345', 3, 2) = '34'
TO_ASCII ('Karel') = 'Karel' quotes and backslashes are properly doubled TRIM (' AB ') = 'AB'
BTRIM ('=‐AB‐=', '‐=') = 'AB' Extract substring matching POSIX regular expression
Character with the given code QUOTE_LITERAL (E'O\'Reilly') = ''O''Reilly'' TRIM (both '‐=' from '=‐AB‐=') = 'AB'
QUOTE_LITERAL (42.5) = "'42.5'" LTRIM ('=‐AB‐=', '‐=') = 'AB‐=' SUBSTRING ('Regex' from '.{3}$') = 'gex'
CHR (65) = 'A' QUOTE_LITERAL ('"ABC"') = '''"ABC"''' TRIM (leading '‐=' from '=‐AB‐=') = 'AB‐='
CHR (1234) = 'Ӓ' RTRIM ('=‐AB‐=', '‐=') = '=‐AB' Extract substring matching SQL regular expression
CHR (NULL) = NULL Return the given string suitably quoted to be used as a TRIM (trailing '‐=' from '=‐AB‐=') = '=‐AB'
string literal in an SQL statement string; or, if the argument SUBSTRING('ABCDE'from'%#"B_D#"_'for'#')='BCD'
Convert string to dest_encoding is null, return NULL
Fill up the string to length by prepending / appending the
SUBSTRING('ABCDE'from'%#"B‐D#"_'for'#')=NULL
characters fill (a space by default)
CONVERT ('Text', 'UTF8', 'LATIN1') = 'Text' QUOTE_NULLABLE (NULL) = NULL Replace all occurrences of one substring with other
QUOTE_NULLABLE (42.5) = "'42.5'" LPAD ('ABC', 6) = ' ABC' substring
Convert string to the database encoding / dest_encoding RPAD ('ABC', 6) = 'ABC '
Convert number to its equivalent hexadecimal
LPAD ('123', 6, '0') = '000123' REPLACE ('A‐B‐C', '‐','+') = 'A+B+C'
CONVERT_FROM ('Text', 'UTF8') = 'Text' representation
RPAD ('C', 3, '+') = 'C++'
CONVERT_TO ('Text', 'UTF8') = 'Text'
TO_HEX (12) = 'c'
Encode / Decode binary data into/from textual TO_HEX (42) = '2a' Replace substring
SEARCH & MATCHING
representation in string TO_HEX (023150) = '5a6e'
OVERLAY('123' placing '‐' from 2 for 3)='1‐' Location of specified substring
TO_HEX (2147483647) = '7fffffff'
OVERLAY('123' placing '‐' from 1 for 2)='‐3'
ENCODE (E'1234', 'base64') = 'MTIzNA=='
OVERLAY('123' placing '‐' from 2 for 1)='1‐3' POSITION ('om' in 'Thomas') = 3
DECODE ('MTIzAAE=', 'base64') = 123
STRPOS ('Thomas', 'om') = 3
Convert the first letter of each word to upper case and the MEASUREMENT Repeat string the specified number of times
Return all captured substrings resulting from matching a
rest to lower case REPEAT ('Pg', 4) = 'PgPgPgPg'
Number of bits in string POSIX regular expression against the string
INITCAP ('hi thomas') = 'Hi Thomas'
BIT_LENGTH ('J') = 8 Any character in string that matches a character in the REGEXP_MATCHES('1,2','(\d),(\d)')={'1','2'}
INITCAP ('all‐in‐all') = 'All‐In‐All' from set is replaced by the corresponding character in the
BIT_LENGTH ('Ö') = 16 REGEXP_MATCHES('1,2','\d','g')={'1'},{'2'}
INITCAP ('all_in_all') = 'All In All' to set
INITCAP ('go2bed') = 'Go2bed' BIT_LENGTH ('jose') = 32
BIT_LENGTH ('JÖSE') = 40 Split string using a POSIX regular expression as the
TRANSLATE ('12321', '12', 'ab') = 'ab3ba' delimiter
Convert string to lower / upper case
Number of characters in string
Replace substring(s) matching a POSIX regular expression REGEXP_SPLIT_TO_ARRAY ('ABC DEF', E'\\s+') =
LOWER ('TOM') = 'tom'
CHAR_LENGTH ('jose') = 4 {ABC,DEF}
UPPER ('tom') = 'TOM' REGEXP_REPLACE('Pipe','p','‐') = 'Pi‐e'
CHARACTER_LENGTH ('jose') = 4 REGEXP_SPLIT_TO_TABLE ('ABC DEF', E'\\s+') =
LENGTH ('jose') = 4 REGEXP_REPLACE('Pipe','p','‐','i') = '‐ipe' 'ABC' 'DEF' (2 rows)
Calculates the MD5 hash of string, returning the result in REGEXP_REPLACE('Pipe','p','‐','g') = 'Pi‐e'
hexadecimal LENGTH ('JÖSE', 'UTF8') = 4
REGEXP_REPLACE('Pipe','p','‐','gi') = '‐i‐e' Return true if the string matches the supplied pattern
Number of bytes in string REGEXP_REPLACE('24 h','\d+','00') = '00 h'
MD5('abc')='900150983cd24fb0d6963f7d28e17f72'
REGEXP_REPLACE('24 h','\s','_') = '24_h' 'ABC' LIKE '_B_' = true
OCTET_LENGTH ('AB') = 2 REGEXP_REPLACE('24 h','[\sh]+','?') = '24?' 'ABC' NOT LIKE '_B_' = false
Current client encoding name
OCTET_LENGTH ('Ö') = 2 REGEXP_REPLACE('\abc','\\.+','*') = '*' 'ABC' SIMILAR TO '[ABC]+' = true
PG_CLIENT_ENCODING () = 'UTF8' OCTET_LENGTH ('') = 4 'ABC' NOT SIMILAR TO '[ABC]+' = false