By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .

Extract sub string from a string is a common operation in data analytics. In Teradata, function SUBSTRING (SUBSTR) and REGEXP_SUBSTR are provided to achieve that. SUBSTR is used to extract string from a specified location while REGEXP_SUBSTR is used to extract string using regular expressions.

Code snippets

SUBSTR/SUBSTRING

-- returns World!
select	substring('Hello World!' from 7);
-- returns World
select	substring('Hello World!' from 7 for 5);

-- returns World!
select	substr('Hello World!', 7);
-- returns World
select	substr('Hello World!',7,5);

REGEXP_SUBSTR

-- returns 1234
select	REGEXP_SUBSTR('Hello 1234 World 5678 ! Kontext.tech 1010 !!!','[\d]+');
-- returns 5678
select	REGEXP_SUBSTR('Hello 1234 World 5678 ! Kontext.tech 1010 !!!','[\d]+',1,2);
-- return null
select	REGEXP_SUBSTR('Hello 1234 World 5678 ! Kontext.tech 1010 !!!','[\d]+',1,4);

-- returns Hello
select	REGEXP_SUBSTR('Hello World! HeLLO World!!!','Hello',1,1,'i');
-- returns HeLLO
select	REGEXP_SUBSTR('Hello World! HeLLO World!!!','Hello',1,2,'i');
-- returns null
select	REGEXP_SUBSTR('Hello World! HeLLO World!!!','Hello',1,2,'c');

In the above examples, occurrence argument can be used to return different values if there are multiple matches. The last argument can be used to specify match arguments. It can be the following values:

  • 'i' = case-insensitive matching
  • 'c' = case sensitive matching
  • 'n' = the period character (match any character) can match the newline character.
  • 'm' = multiple line match
  • 'l' = returns NULL if source string size is more than maximum allowed size (currently 16 MB). This can be used to avoid errors when match with big string.
  • 'x' = ignore whitespace.

The argument can contain more than one character. For example:

select	REGEXP_SUBSTR('Hello World! HeLLO World!!!','Hello',1,2,'im');

By default, the match is case sensitive and a period does not match the newline character and source string is treated as a single line.

* This page is subject to Site terms.

More from Kontext

local_offer teradata local_offer SQL

visibility 6
thumb_up 0
access_time 2 days 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. ANSI SQL REPLACE function REPLACE function is commonly implemented in many other SQL databases such as ...

open_in_new View open_in_new Code snippets

local_offer pyspark local_offer spark-2-x local_offer teradata local_offer SQL Server

visibility 59
thumb_up 0
access_time 17 days ago

In my previous article about  Connect to SQL Server in Spark (PySpark) , I mentioned the ways t...

open_in_new View open_in_new Spark + PySpark

local_offer teradata local_offer SQL

visibility 26
thumb_up 1
access_time 22 days ago

COALESCE function in Teradata returns NULL if all arguments evaluate to null; otherwise it returns the value of the first non-null argument. NULLIF is to used evaluate two expressions and returns NULL if the two arguments are equal otherwise if returns the first arguments. IS NULL i...

open_in_new View open_in_new Code snippets

local_offer teradata local_offer SQL

visibility 19
thumb_up 0
access_time 22 days ago

In SQL Server, TRY_CAST function returns a value cast to a specified data type is the cast is successful or null is the cast is not successful.  In Teradata, the equivalent function is TRYCAST. Code snippets -- return int 1030 SELECT TRYCAST('01030' AS int); -- retur...

open_in_new View open_in_new Code snippets

info About author

Kontext dark theme mode

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward
Kontext Column

Kontext Column

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

Learn more arrow_forward
info Follow us on Twitter to get the latest article updates. Follow us