Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 16 February 2024

SUBSTR in TERADATA

In Teradata, the SUBSTRING function is used to extract a substring from a string. It operates similarly to the SUBSTR function in other SQL databases like Oracle and MySQL.

Here's the syntax for the SUBSTRING function in Teradata:

SUBSTRING(string_expression FROM start_position [FOR length])

- string_expression: This is the string from which you want to extract the substring.

- start_position: This is the starting position within the string where the substring extraction should begin. The first character in the string has a position of 1.

- length (optional): This is the length of the substring to extract. If omitted, the SUBSTRING function returns all characters from the start_position to the end of the string.

Let's see an example to understand how SUBSTRING works in Teradata:

Suppose we have a string 'Hello, World!', and we want to extract a substring starting from the 7th position with a length of 5 characters.

SELECT SUBSTRING('Hello, World!' FROM 7 FOR 5) AS substring_result;

This query will return the result:

substring_result

World

In this example:

- We use the SUBSTRING function to extract a substring from the string 'Hello, World!'.

- We specify the starting position as 7, and the length as 5.

- The extracted substring starts from the 7th position ('W') and has a length of 5 characters ('World').

If you omit the length parameter, SUBSTRING will return all characters from the start_position to the end of the string. For example:

SELECT SUBSTRING('Hello, World!' FROM 7) AS substring_result;

This would return 'World!', as it starts from the 7th position and continues to the end of the string.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.