Teradata SQL - Get Rid of Spaces from String
In article Teradata SQL - Use OREPLACE to Replace or Remove Characters, it shows how to use OREPLACE function in Teradata to replace or remove strings. This page will show one more alternative way of doing that using regular expressions.
Remove spaces using OREPLACE
select oreplace('Kontext is a website for data engineers.',' ', '')
You can also use latin ASCII number as search or replace string. CHR function returns the Latin ASCII character given a numeric code value.
select oreplace('Kontext is a website for data engineers.',CHR(32), '')
The ASCII decimal number for space is 32 thus the above code snippet will return the same results.
Remove tabs using OREPLACE
select oreplace('There is a tab in between.',CHR(9), '')
Remove spaces using REGEXP_REPLACE
You can also use regular expressions to remove certain characters.
select REGEXP_REPLACE('Kontext is a website for data engineers.','\s', '')
In the above code snippet, regular expression \s matches with all the spaces.
Replace only at least two consecutive spaces
select REGEXP_REPLACE('Kontext is a website for data engineers.','[\s]{2,}', '')
Regular expression [\s]{2,} matches at least two space characters; thus only those ones will be replaced.
Other regular expression functions in Teradata
There are other regular expression related functions in Teradata such as:
- REGEXP_SUBSTR
- REGEXP_INSTR
- REGEXP_SIMILAR
- REGEXP_SPLIT_TO_TABLE
Refer to the following official page for more details:
Teradata Regular Expression Functions