使用 Teradata SPL 反转字符串 [英] Reversing a string using Teradata SPL

查看:93
本文介绍了使用 Teradata SPL 反转字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Teradata 存储过程语言 (SPL) 反转字符串?要求是复制 SQL-Server replace() 函数.这可以通过在 Teradata 中编写相应的 UDF 来实现,但我想使用过程来实现.

How can I reverse a string using Teradata Stored Procedure Language (SPL) ? The requirement was to replicate the SQL-Server replace() function. This could be achieved by writing a corresponding UDF in Teradata but I want to achieve the same using a Procedure.

然后可以使用该过程来初始化变量,然后再将其用于具有 reverse() 的实际语句.

The procedure could be then used to initialize the variable before its use in the actual statement having reverse().

推荐答案

这可以使用以下存储过程来实现.

This can be achieved using he following Stored Procedure.

其背后的主要逻辑是,在每次迭代中(迭代次数等于字符串的长度),我们将最后一个字母连接到一个变量中.

The main logic behind it is that in each iteration (number of iterations equal to the length of the string), we concatinate the last alphabet to a variable.

REPLACE PROCEDURE database.REVERSE_STRING(INOUT STRING VARCHAR(20))
BEGIN
--Declare Variables
DECLARE STRING_LOCAL  VARCHAR(20); -- Local Copy
DECLARE STRING_LEN VARCHAR(20); -- String Length

--Initialize Variables
SET STRING_LEN = CHARACTER_LENGTH(STRING); -- Find Out the length of string
SET STRING_LOCAL = ''; -- Initialize local copy

--Main Loop
WHILE(STRING_LEN > 0)
DO
    SET STRING_LOCAL = STRING_LOCAL || SUBSTR(STRING,STRING_LEN,1); -- In each Iteration copy last alphabet and con cat with STRING_LOCAL
    SET STRING_LEN = STRING_LEN - 1; -- Decrease Iterator Value
END WHILE;

SET STRING = TRIM(STRING_LOCAL); -- Return reversed string

END;

这篇关于使用 Teradata SPL 反转字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆