📜  Teradata字符串操作

📅  最后修改于: 2021-01-11 11:20:43             🧑  作者: Mango

Teradata字符串操作

Teradata提供了几种操作字符串的功能。这些功能与ANSI标准兼容。

大多数标准字符串函数以及对这些函数的Teradata扩展也都支持Teradata字符串函数。

String function Explanation
Concat (string1, …, stringN) It returns the concatenation of two or more string values. This function provides the same functionality as the SQL-standard concatenation operator (||).
Length (string) It returns the number of characters in the string.
Lower (string) It converts a string to lower case.
Upper (string) It converts a string to upper case.
Lpad (string, size, padstring) Pads the left side of the string with characters to create a new string.
Rpad (string, size, padstring) Pads the right side of the string with characters to create a new string.
Trim (string) It removes leading and trailing whitespace from the given string.
Ltrim (string) It removes leading whitespaces from the string.
Rtrim (string) It removes trailing whitespaces from the string.
Replace (string, search) It removes the search string from the given string.
Replace (string, search, replace) It replaces all instances of search with replacing string.
Reverse (string) It returns string characters in reverse order.
Split (string, delimiter) Split given string on the delimiter. This function returns an array of string.
Strops (string, substring) It returns the staring position first instance of a substring in a given string.
Position (substring IN string) It returns the staring position first instance of a substring in a given string.
Substr (string, start, length) It returns a substring of string that begins at the start position and is length characters long.
Chr (n) It returns the character with the specified ASCII value.
to_utf8 (string) It encodes a string into a UTF-8 varbinary representation.
from_utf8 (binary) It decodes a UTF-8 encoded string from binary.
Select translate (string, from, to); It can replace any character in the string that matches a character in the form set with the corresponding character in the set.
Index (string) It locates the position of a character in a string (Teradata extension).

上下功能

UPPER和LOWER函数分别将字符列值全部转换为大写和小写。 UPPER和LOWER符合ANSI。

句法

UPPER (expression) -> returns expression as uppercase 
LOWER (expression) -> returns expression as lowercase 

下面的例子将“罗伯特”字符串转换的大写字符串。

        /*create a table called NAMES */
CREATE TABLE NAMES(Name text);
       /*create few records in this table */
SELECT UPPER('Robert');
       /*Query completed. One row found. One column returned */
       /*Total elapsed time was 0.00 second */
       /*Display all the records from the table */
SELECT * FROM NAMES;

执行完上面的代码后,它将给出以下输出。

ROBERT

现在,在同一示例中,我们将在小写字符串字符串中转换相同的“ ROBERT”字符串。

SELECT LOWER('ROBERT');
      /*Query completed. One row found. One column returned */
     /*Total elapsed time was 0.00 second */

输出量

robert

CHARACTER_LENGTH函数

所述CHARACTER_LENGTH函数返回<字符>表达式的字符的数目。

  • 结果将是代表长度的整数。
  • 对于定长字符,结果将相同。
  • 对于可变长度字符,结果将有所不同。
  • 空格是有效字符,因此长度将计入空格。

句法

CHARACTER_LENGTH (expression)

下面的例子将返回“罗伯特”字符串的字符数。

SELECT CHARACTER_LENGTH('Robert');
          /* Query completed. One row found. One column returned */
         /* Total elapsed time was 0.00 second */

执行上面的代码,它告诉“ Robert”字符串的长度,如下所示。

6

TRIM功能

TRIM函数用于从表达式中删除特定的前导或尾随或两者之间的空间。 TRIM是ANSI标准。

句法

TRIM ([LEADING | BOTH | TRAILING] [trim_character ] FROM expression)

以下示例从“ Robert”字符串的两端删除空格。

SELECT TRIM('   Robert    ');
            /* Query completed. One row found. One column returned */
            /* Total elapsed time was 0.00 second */

当我们执行上面的代码时,它将从字符串的两端修剪掉现有的空间并给出以下输出。

Robert

位置功能

POSITION函数用于返回子字符串在字符串的位置。仅返回第一次出现的字符串的位置。

句法

POSITION (substr IN str)

下面的示例将在“ Robert”字符串返回出现的“ e”。

SELECT POSITION("e" IN "Robert");
          /* Query completed. One row found. One column returned */
         /* Total elapsed time was 0.00 second */

执行完上面的代码后,它将在“ Robert”字符串找到“ e”子字符串的位置作为输出。

4

SUBSTRING功能

SUBSTRING函数用于从给定字符串的特定位置返回指定数量的字符。 SUBSTRING函数是ANSI标准。

句法

SUBSTRING (str FROM pos FOR len)

它返回一个字符串(str),从位置(pos)开始,以字符长度(len)。

以下示例返回从3个字符第1个位置处的字符。

SELECT SUBSTRING('Robert' FROM 1 FOR 3);
         /* Query completed. One row found. One column returned */
        /* Total elapsed time was 0.00 second */

上面的代码从字符串“ Robert”的第一个位置返回3个字符作为输出。

Rob