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 python

visibility 636
thumb_up 1
access_time 3 months ago

Pandas is commonly used by Python users to perform data operations. In many scenarios, the results need to be saved to a storage like Teradata. This article shows you how to do that easily using JayDeBeApi or  ...

open_in_new Spark + PySpark

local_offer teradata local_offer SQL

visibility 60
thumb_up 0
access_time 3 months ago

In SQL Server, we can use TRUNCATE statement to clear all the records in a table and it usually performs better compared with DELETE statements as no transaction log for each individual row deletion. The syntax looks like the following: TRUNCATE TABLE { database_name.schema_name.tab...

open_in_new Code snippets

local_offer teradata local_offer python local_offer Java

visibility 353
thumb_up 0
access_time 3 months ago

Python JayDeBeApi module allows you to connect from Python to Teradata databases using Java JDBC drivers. In article Connect to Teradata database through Python , I showed ho...

open_in_new Python Programming

local_offer teradata local_offer SQL

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

open_in_new Code snippets

info About author

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

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


Learn more arrow_forward