2011-11-08 36 views

答えて

10

文字列を分割する方法のサンプルを行うと、 wriサブストリングをテーブルに入れてください:

create procedure SPLIT_STRING (
    AINPUT varchar(8192)) 
as 
declare variable LASTPOS integer; 
declare variable NEXTPOS integer; 
declare variable TEMPSTR varchar(8192); 
begin 
    AINPUT = :AINPUT || ','; 
    LASTPOS = 1; 
    NEXTPOS = position(',', :AINPUT, LASTPOS); 
    while (:NEXTPOS > 1) do 
    begin 
    TEMPSTR = substring(:AINPUT from :LASTPOS for :NEXTPOS - :LASTPOS); 
    insert into new_table("VALUE") values(:TEMPSTR); 
    LASTPOS = :NEXTPOS + 1; 
    NEXTPOS = position(',', :AINPUT, LASTPOS); 
    end 
    suspend; 
end 
+0

クール。どうもありがとうございました。 –

+0

注記: ''、1,2''のような' AINPUT'の結果は返しませんが、 '' 1,2、' 'のような' AINPUT'の3つの部分文字列を返します。 – Wodzu

4
+0

ようこそを発表しました!これは理論的に質問に答えるかもしれませんが、答えの本質的な部分をここに含め、参照用のリンクを提供することが望ましいでしょう(http://meta.stackexchange.com/q/8259)。ありがとう – codingbadger

10

変更されたマイケルのバージョンを投稿しています。多分誰かにとって役に立ちます。

変更は、次のとおり

  1. SPLIT_STRINGは、選択手順です。
  2. カスタム区切り文字は使用できます。
  3. 区切り文字がP_STRINGの最初の文字である場合も解析します。
set term^; 
create procedure split_string (
    p_string varchar(32000), 
    p_splitter char(1)) 
returns (
    part varchar(32000) 
) 
as 
    declare variable lastpos integer; 
    declare variable nextpos integer; 
begin 
    p_string = :p_string || :p_splitter; 
    lastpos = 1; 
    nextpos = position(:p_splitter, :p_string, lastpos); 
    if (lastpos = nextpos) then 
     begin 
      part = substring(:p_string from :lastpos for :nextpos - :lastpos); 
      suspend; 
      lastpos = :nextpos + 1; 
      nextpos = position(:p_splitter, :p_string, lastpos); 
     end 
    while (:nextpos > 1) do 
     begin 
      part = substring(:p_string from :lastpos for :nextpos - :lastpos); 
      lastpos = :nextpos + 1; 
      nextpos = position(:p_splitter, :p_string, lastpos); 
      suspend; 
     end 
end^ 
set term ;^
+0

素晴らしい仕事@MartjinPieters :) – hims056

+0

ありがとう@MartijnPieters。 – Wodzu

3

それは32000の原因に私のFirebirdサーバーのVARCHARのサイズ宣言の中で、一つのことを除いて良さそうですので注意してください例外を「実装制限を超過しました」。私の代わりにBLOBのSUB_TYPEのTEXTを:)使用することをお勧め

2

私が使用して同様のソリューションは、スタックオーバーフローに智異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 
関連する問題