8
A
回答
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
4
使用 位置 http://www.firebirdsql.org/refdocs/langrefupd21-intfunc-position.html
和
SUSTRING http://www.firebirdsql.org/refdocs/langrefupd21-intfunc-substring.html
在WHILE功能DO語句
+0
歡迎堆棧溢出!雖然這可能在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包括答案的重要部分,並提供供參考的鏈接。感謝 – codingbadger
10
我發佈了修改後的邁克爾版本,也許它會對某人有用。
的變化是:
- SPLIT_STRING是可選步驟。
- 自定義分隔符是可能的。
- 它還分析了分隔符是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 ;^
3
它看起來只是有一點好,我的火鳥服務器的Varchar大小申報32000事業「實施超限」的例外,所以要小心。我建議使用BLOB SUB_TYPE文本而不是:)
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
相關問題
- 1. 拆分逗號分隔的字符串
- 2. 拆分逗號分隔字符串5
- 3. TSQL拆分逗號分隔字符串
- 4. 拆分逗號分隔字符串/值
- 5. 拆分逗號分隔字符串
- 6. 如何在Oracle中拆分逗號分隔的字符串
- 7. 將逗號分隔的字符串傳遞給存儲過程?
- 8. 如何在MySQL存儲過程中拆分逗號分隔的文本
- 9. 在PL/SQL存儲過程中分割逗號分隔的字符串
- 10. 如何拆分用逗號分隔的字符串?
- 11. 拆分逗號分隔字符串中的Bash
- 12. 拆分逗號分隔的多個字符串在C#
- 13. 如何逗號分隔字符串存儲到一個表
- 14. 拆分逗號分隔的字符串,但忽略逗號整個字
- 15. 拆分逗號分隔字符串轉換成排在MySQL
- 16. 分離逗號分隔的字符串
- 17. SQL:用查詢拆分逗號分隔的字符串列表?
- 18. 將逗號分隔的字符串拆分爲列
- 19. 如何在Oracle存儲過程中獲取逗號分隔值
- 20. 拆分字段的逗號分隔值
- 21. 轉換被逗號分隔值,以逗號分隔字符串
- 22. 逗號分隔符號字符串
- 23. 包含逗號與逗號分隔符的分割字符串
- 24. 拆分逗號分隔字符串,字符串中丟棄一些值
- 25. 搜索逗號IDS分隔字符串以逗號分隔字符串
- 26. 如何拆分逗號分隔變量?
- 27. 逗號分隔字符串分割
- 28. 將逗號分隔的字符串拆分爲較小的字符串
- 29. 拆分逗號分隔字符串(PHP +正則表達式)
- 30. 拆分逗號分隔字符串得到「」
很酷。非常感謝你。 –
需要注意的是:它不會像'',1,2''那樣返回'AINPUT'的任何結果,但它會像'1,2'一樣返回'AINPUT'的三個子串。 – Wodzu