Teradata SQL - Get Rid of Spaces from String

access_time 7 months ago visibility701 comment 0

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

info Last modified by Administrator 5 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

More from Kontext

visibility 701
thumb_up 0
access_time 7 months ago

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. select oreplace('Kontext is a website for data ...

visibility 7
thumb_up 0
access_time 30 days ago

In Teradata, error 'no more room in database' can be common especially when data is not evenly distributed (i.e. high skewness). Sometimes when this error occurs, it might be just because of one or a few AMPs are full. For example, assuming a database with permanent space allocated as 100GB and ...

visibility 1804
thumb_up 0
access_time 10 months ago

OREPLACE functions in Teradata can be used to replace or remove characters from a string. OREPACE is Teradata's extension to ASNI SQL. The usual REPLACE function is not available. REPLACE function is commonly implemented in many other SQL databases such as SQL Server, MySQL, BigQuery, Oracle, etc.