access_time 11 months ago visibility628 comment 0

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


-- 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);


-- 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.

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 4
thumb_up 0
access_time 4 days ago

Teradata Administrator tool used to have a function to show rights held by a database. You can also directly use SQL to query that instead of relying on the GUI tool. select * from dbc.allrights where UserName='TestDb'; The above code snippet returns all the access rights held by database ...

visibility 428
thumb_up 0
access_time 6 months ago

This page shows how to create a temporary Hive table via Hive SQL (HQL). Example: CREATE TEMPORARY TABLE temp_customer (`cust_id` int, `name` string,`created_date` date) STORED AS PARQUET; Temporary Hive tables are only visible to the creation session and will be deleted automatically when ...

visibility 4
thumb_up 0
access_time 4 days ago

The following code snippet checks all roles with access on certain Teradata table or view.  select * from dbc.allrolerights where databasename='TestDb' and tablename = 'test_table'; Sample output: To check certain role's access on database object in Teradata, use the following query ...