15.00 - STRTOK - Teradata Database

Teradata Database SQL Functions, Operators, Expressions, and Predicates

Product
Teradata Database
Release Number
15.00
Content Type
Programming Reference
Publication ID
B035-1145-015K
Language
English (United States)
Last Update
2018-09-24

STRTOK

Purpose  

Splits instring into tokens based on the specified list of delimiter characters and returns the nth token, where n is specified by the tokennum argument.

Syntax  

where:

 

Syntax element…

Specifies…

TD_SYSFNLIB

the name of the database where the function is located.

instring

a character string or string expression.

delimiter

a list of delimiter characters. Each character in the string is considered a delimiter character.

If not specified, the default delimiter character is a space character.

tokennum

the ordinal token to return.

If not specified, the default value is 1.

ANSI Compliance

This is a Teradata extension to the ANSI SQL:2011 standard.

Invocation

STRTOK is an embedded services system function. For information on activating and invoking embedded services functions, see “Embedded Services System Functions” on page 24.

Argument Types and Rules

Expressions passed to this function must have the following data types:

  • instring = VARCHAR(32000) or CLOB
  • delimiter = VARCHAR(64)
  • tokennum = INTEGER
  • token size = The maximum limit of any token returned is 256.
  • You can also pass arguments with data types that can be converted to the above types using the implicit data type conversion rules that apply to UDFs.

    Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to the required data type following the UDF implicit conversion rules, it must be explicitly cast.

    For details, see “Compatible Types” in SQL External Routine Programming.

    Result Type

    The result data type is VARCHAR and the character set is the same as that of the instring argument. For example, if the instring argument has a data type of VARCHAR CHARACTER SET UNICODE, the result data type is VARCHAR CHARACTER SET UNICODE.

    Usage Notes  

    STRTOK has similar semantics as the standard C/C++ library STRTOK function.

    If instring contains fewer tokens than tokennum, the function returns a zero-length string.

    If either instring or delimiter is NULL, the function returns NULL.

    Example

    Consider the following table and inserted data.

       CREATE TABLE t (id INTEGER, str VARCHAR(256));
     
       INSERT INTO t VALUES (1,'Teradata-Warehouse 13.10 - Combine 2 powerful forms of business intelligence (BI).'); 
     
       INSERT INTO t VALUES (2,'http://www.teradata.com/');

    The following query returns the 3rd token from the t.str string value. There are only 2 tokens in the second string, so the function returns a zero-length string for that row.

    SELECT id, STRTOK(t.str, ' -/', 3) FROM t;

    The output from the query is:

             id  token
    -----------  ----------------------------------------------------------
              1  13.10
              2