Raymond Raymond / Code Snippets & Tips

Teradata SQL - Get Rid of Spaces from String

event 2020-07-26 visibility 9,135 comment 0 insights toc
insights Stats

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:


Refer to the following official page for more details:

Teradata Regular Expression Functions

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts