0

Does someone knows how can I get that result bellow in DB2?

Query

Example : 

    SELECT * FROM TABLE(FunctionName('1||2||3',||)) ;

First one are parameters with values and second one is delimiter.

Result:

    Column

    1
    2
    3

Values that I put on that query are based on parameters and delimiters.

As I said above, rows is break by Pipe. I dont have huge background on PL/SQL.

Im using DB2 v11.1

2
  • Do you mean ` SELECT * FROM TABLE(FunctionName('1||2||3','||')) ;` Commented Dec 27, 2017 at 13:39
  • Possible duplicate of stackoverflow.com/questions/18961996/… Commented Dec 27, 2017 at 13:40

1 Answer 1

1

So ive been facing that problem , and i found a solution for it :

CREATE OR REPLACE FUNCTION Test(
Data_1 CLOB(1M), Delimtator VARCHAR(12))
RETURNS TABLE (
FieldData varchar(2048))
LANGUAGE SQL
BEGIN
    DECLARE dInic INTEGER DEFAULT 1 ;
    DECLARE dFim  INTEGER DEFAULT 0 ;
    DECLARE Rowid1 INTEGER DEFAULT 0;
    DECLARE Campo VARCHAR(2048);

    IF Data_1 IS NULL THEN
        RETURN;
    END IF;

    SET dFim=LOCATE(Delimtator,Data_1);

    WHILE dFim>0 DO
     SET Campo=SUBSTRING(Data_1,dInic,dFim-dInic);
        PIPE (Campo);
        SET dInic=dFim+LENGTH(Delimtator);
        SET dFim=LOCATE(Delimtator,Data_1,dFim+LENGTH(Delimtator));
    END WHILE;

    SET Campo=SUBSTRING(Data_1, dInic,LENGTH(Data_1)-dInic+1);
    PIPE (Campo);
    RETURN;
END@
Sign up to request clarification or add additional context in comments.

1 Comment

I like that you did this. Could you comment the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.