Example: Using Concatenation, SUBSTRING, and INDEX - Advanced SQL Engine - Teradata Database

SQL Functions, Expressions, and Predicates

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
17.00
Published
June 2020
Language
English (United States)
Last Update
2021-01-30
dita:mapPath
tpt1555966086716.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1145
lifecycle
previous
Product Category
Teradata Vantageā„¢

A somewhat more complex construction employing concatenation, SUBSTRING, and INDEX might be more instructive. Suppose the employee table contains the following values.

   empno      name       
   ---------- -----------
   10021      Smith T
   10007      Aguilar J
   10018      Russell S
   10011      Chin M
   10019      Newman P

You can transpose the form of the names from the name column selected from the employee table and change the punctuation in the report using the following query:

   SELECT empno, 
   SUBSTRING(name FROM INDEX(name,' ')+1 FOR 1)||
'. '||
   SUBSTRING(name FROM 1 FOR INDEX(name, ' ')-1) 
   (TITLE 'Emp Name') 
   FROM employee ; 

The system returns the following report.

   empno      Emp Name       
   ---------- --------------
   10021      T. Smith 
   10007      J. Aguilar 
   10018      S. Russell 
   10011      M. Chin 
   10019      P. Newman