Reversing a string using Teradata SPL

丶灬走出姿态 提交于 2020-01-06 01:52:07

问题


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.

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


回答1:


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;


来源:https://stackoverflow.com/questions/11930925/reversing-a-string-using-teradata-spl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!