Teradata SQL - Get Rid of Spaces from String

access_time 4 months ago visibility192 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 at 2 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

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.


Learn more arrow_forward

More from Kontext

local_offer teradata local_offer teradata-utilities

visibility 28
thumb_up 0
access_time 2 months ago

BTEQ is a Teradata utility tool that can be used to run Teradata SQL statements incl. DDL, DML, etc. It can also be used to import data from text file into Teradata databases. Like TPT and FASTLOAD, it can run in both batch and interactive modes. This article demonstrates how to load XML file into ...

local_offer teradata local_offer fastload local_offer teradata-utilities

visibility 41
thumb_up 0
access_time 2 months ago

Teradata FastLoad can be used to load CSV/TSV or other delimited files into database. Refer to article  Teradata FastLoad - Load CSV File  for more details about how to load CSV into Teradata. This article shows how to skip header line or multiple lines in the input file.  Create a ...

local_offer teradata local_offer fastload local_offer teradata-utilities

visibility 154
thumb_up 0
access_time 2 months ago

Teradata FastLoad is a command line utility that can be used to load large amount of data into an empty table on Teradata database. The performance will be greater than line by line or batch processing mechanism. This article provides example of using FastLoad to load CSV file into Teradata ...

About column

Code snippets and tips for various programming languages/frameworks.

rss_feed Subscribe RSS