In PostgreSQL, we can use the pg_client_encoding()
function to get the current client encoding name.
Tag: string functions
Understanding the RIGHT() Function in PostgreSQL
In PostgreSQL the right()
function returns the specified number of rightmost characters in a given string.
We have the option of specifying the number of characters to return from the right or the number of characters to omit from the left. We do this by specifying a positive integer (to return n number of rightmost characters) or a negative integer (to return everything except n leftmost characters).
Continue readingA Quick Look at the LEFT() Function in PostgreSQL
In PostgreSQL we can use the left()
function to get the specified number of leftmost characters in a given string.
We have the option of specifying the number of characters to return from the left or the number of characters to omit from the right. We do this by specifying a positive integer (to return n number of leftmost characters) or a negative integer (to return everything except n rightmost characters).
Continue readingHow REGEXP_REPLACE() Works in PostgreSQL
In PostgreSQL, we can use the regexp_replace()
function to replace a substring within a given string, based on a given POSIX regular expression. We can specify that all matches are replaced or just the first match.
We pass the string as the first argument, the pattern as the second, and the replacement text as the third argument. We also have the option of specifying the start position as the fourth argument, and we can specify a flag to determine how the function behaves.
Continue readingHow the CHR() Function Works in PostgreSQL
In PostgreSQL, we can use the chr()
function to return a character based on its code.
The code is provided as an integer argument, and the function returns the character that the code represents. When using UTF8 encoding the argument is treated as a Unicode code point, otherwise it must designate an ASCII character.
Continue readingUnderstanding PostgreSQL’s UNISTR() Function
PostgreSQL has a unistr()
function that evaluates escaped Unicode characters in its argument and returns the result as text.
Understanding PostgreSQL’s TRANSLATE() Function
PostgreSQL has a translate()
function which replaces certain characters in a string with certain other characters.
We specify the characters in the string that we want to replace, as well as the characters that we want to replace them with. The function then performs the translation and returns the string with the replaced characters.
Continue readingA Quick Look at TO_ASCII() in PostgreSQL
In PostgreSQL, we can use the to_ascii()
function to convert a string to ASCII from another encoding. This usually involves dropping any accents (diacritic signs) from the original string.
We pass the string as an argument to the function. We also have the option of specifying the encoding (either its name or its number).
Continue readingOverview of the REPEAT() Function in PostgreSQL
We can use PostgreSQL’s repeat()
function to repeat a string multiple times. We pass the string to the function, along with an integer that specifies how many times we want it repeated, and it returns the string repeated that many times.
5 String Functions that Return Length in PostgreSQL
PostgreSQL provides us with a handful of string functions that return the length of a given string.
But the result between these functions can be different, depending on which function we use. That’s because the “length” can be different, depending on what we’re measuring. Are we talking about the number of characters in the string? Or the number of bytes in the string? Or perhaps we want to know the number of bits in the string.
The function we use will depend on which of the above we’re trying to measure. Below are five functions that cater for each of the above questions.
Continue reading