问题
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