How to split comma separated string inside stored procedure?

后端 未结 6 1541
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 23:41

How to split comma separated string into strings inside store procedure and insert them into a table field?

Using Firebird 2.5

6条回答
  •  春和景丽
    2020-12-16 23:46

    A similar solution I use, published a while ago by Jiri Cincura http://blog.cincura.net/232347-tokenize-string-in-sql-firebird-syntax/

    recreate procedure Tokenize(input varchar(1024), token char(1))
    returns (result varchar(255))
    as
    declare newpos int;
    declare oldpos int;
    begin
      oldpos = 1;
      newpos = 1;
      while (1 = 1) do
      begin
        newpos = position(token, input, oldpos);
        if (newpos > 0) then
        begin
          result = substring(input from oldpos for newpos - oldpos);
          suspend;
          oldpos = newpos + 1;
        end
        else if (oldpos - 1 < char_length(input)) then
        begin
          result = substring(input from oldpos);
          suspend;
          break;
        end
        else
        begin
          break;
        end
      end
    end
    

提交回复
热议问题