Function replace_encoding
The function has three implementations.
All implementations depend on the function is_encoding, the function is included in this repository.
replace_encoding(s text, e text)
The function takes one parameter with characters to be checked and replaced with an empty string, if they are not part of the encoding given in the second parameter.
Example
SELECT replace_encoding(
'Some characters, ğ is Turkish and not latin1',
'latin1'
) AS res;
Result:
| res |
|---|
| Some characters, is Turkish and not latin1 |
replace_encoding(s text, e text, replacement text)
The function takes a third parameter which is used to replace all characters which are not part of the encoding given in parameter 2.
Example
SELECT replace_encoding(
'Some characters, ğ is Turkish and not latin1 and replaced with a g',
'latin1',
'g'
) AS res;
Result:
| res |
|---|
| Some characters, g is Turkish and not latin1 and replaced with a g |
replace_encoding(s text, s_search text[], s_replace text[])
The function takes as first parameter a string which may or may not have none latin1 characters. The second parameter is an arrays containing all characters, that should be replaced. The third parameter is an array, too. The characters defined in s_search are replaced with the characters in s_replace, it takes the position in the array to identify which character should be replaced by which one.
Example
-- First identify the characters which should be replaced, which are {ğ,Ƶ}
SELECT return_not_part_of_latin1('ağbƵcğeƵ') AS res;
-- The ğ will be replaced whit a g and the Ƶ with a Z}
SELECT 'ağbƵcğeƵ' AS original
, replace_encoding(
'ağbƵcğeƵ',
string_to_array('ğ,Ƶ', ','),
string_to_array('g,Z', ',')
) AS res;
Result:
| original | res |
|---|---|
| ağbƵcğeƵ | agbZcgeZ |