sqlserver 分割字串為陣列
阿新 • • 發佈:2019-01-05
---返回字串按照指定分隔符分割後的陣列的長度
CREATE function Get_StrArrayLength
(
@str varchar(1024), --要分割的字串
@split varchar(10) --分隔符號
)
returns int
as
begin
declare @location int
declare @start int
declare @length int
set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @ [email protected]+1
set @location=charindex(@split,@str,@start)
set @[email protected]+1
end
return @length
end ;
----按照指定的分隔符分割字串為陣列形式,然後獲取指定元素。注: 元素個數從1開始
CREATE function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字串
@split varchar(10), --分隔符號
@index int --取第幾個元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @ [email protected][email protected]
set @location=charindex(@split,@str,@start)
set @[email protected]+1
end
if @location =0 select @location =len(@str)+1
--這兒存在兩種情況:1、字串不存在分隔符號 2、字串中存在分隔符號,跳出while迴圈後,@location為0,那預設為字串後邊有一個分隔符號。
return substring(@str,@start,@[email protected])
end ;
---測試。注意: P_GET_HttpRequestData 為sqlserver獲取http請求資料的儲存過程,在Sqlserver處理Http請求日誌中有講述。
CREATE PROCEDURE IPCC_IVR_QUERYCARDONECINFO(@dataStr VARCHAR(255),@dataIndex VARCHAR(4),@outText1 VARCHAR(255) OUT,@outText2 VARCHAR(255) OUT,@outText3 VARCHAR(255) OUT)
AS
BEGIN
DECLARE @returnText VARCHAR(500)
DECLARE @status int
DECLARE @urlStr VARCHAR(255)
SET @urlStr = 'http://172.17.5.14:9090/df/sendDataToIVR.action?parametersStr='[email protected] +'#'[email protected]+'&ifaceType=2';
EXEC P_GET_HttpRequestData @urlStr, @status OUTPUT, @returnText OUTPUT;
SET @outText1 = dbo.Get_StrArrayStrOfIndex(@returnText,',',1);
SET @outText2 = dbo.Get_StrArrayStrOfIndex(@returnText,',',2);
SET @outText3 = dbo.Get_StrArrayStrOfIndex(@returnText,',',3);
END;
---返回字串按照指定分隔符分割後的陣列的長度 CREATE function Get_StrArrayLength ( @str varchar(1024), --要分割的字串 @split varchar(10) --分隔符號 ) returns int as begin declare @location int declare @start int declare @length int set @str=ltrim(rtrim(@str)) set @location=charindex(@split,@str) set @length=1 while @location<>0 begin set @[email protected]+1 set @location=charindex(@split,@str,@start) set @[email protected]+1 end return @length end ; ----按照指定的分隔符分割字串為陣列形式,然後獲取指定元素。注: 元素個數從1開始 CREATE function Get_StrArrayStrOfIndex ( @str varchar(1024), --要分割的字串 @split varchar(10), --分隔符號 @index int --取第幾個元素 ) returns varchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int set @str=ltrim(rtrim(@str)) set @start=1 set @next=1 set @seed=len(@split) set @location=charindex(@split,@str) while @location<>0 and @index>@next begin set @[email protected][email protected] set @location=charindex(@split,@str,@start) set @[email protected]+1 end if @location =0 select @location =len(@str)+1 --這兒存在兩種情況:1、字串不存在分隔符號 2、字串中存在分隔符號,跳出while迴圈後,@location為0,那預設為字串後邊有一個分隔符號。 return substring(@str,@start,@[email protected]) end ; ---測試。注意: P_GET_HttpRequestData 為sqlserver獲取http請求資料的儲存過程,在Sqlserver處理Http請求日誌中有講述。 CREATE PROCEDURE IPCC_IVR_QUERYCARDONECINFO(@dataStr VARCHAR(255),@dataIndex VARCHAR(4),@outText1 VARCHAR(255) OUT,@outText2 VARCHAR(255) OUT,@outText3 VARCHAR(255) OUT) AS BEGIN DECLARE @returnText VARCHAR(500) DECLARE @status int DECLARE @urlStr VARCHAR(255) SET @urlStr = 'http://172.17.5.14:9090/df/sendDataToIVR.action?parametersStr='[email protected] +'#'[email protected]+'&ifaceType=2'; EXEC P_GET_HttpRequestData @urlStr, @status OUTPUT, @returnText OUTPUT; SET @outText1 = dbo.Get_StrArrayStrOfIndex(@returnText,',',1); SET @outText2 = dbo.Get_StrArrayStrOfIndex(@returnText,',',2); SET @outText3 = dbo.Get_StrArrayStrOfIndex(@returnText,',',3); END;
---返回字串按照指定分隔符分割後的陣列的長度 CREATE function Get_StrArrayLength ( @str varchar(1024), --要分割的字串 @split varchar(10) --分隔符號 ) returns int as begin declare @location int declare @start int declare @length int set @str=ltrim(rtrim(@str)) set @location=charindex(@split,@str) set @length=1 while @location<>0 begin set @[email protected]+1 set @location=charindex(@split,@str,@start) set @[email protected]+1 end return @length end ; ----按照指定的分隔符分割字串為陣列形式,然後獲取指定元素。注: 元素個數從1開始 CREATE function Get_StrArrayStrOfIndex ( @str varchar(1024), --要分割的字串 @split varchar(10), --分隔符號 @index int --取第幾個元素 ) returns varchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int set @str=ltrim(rtrim(@str)) set @start=1 set @next=1 set @seed=len(@split) set @location=charindex(@split,@str) while @location<>0 and @index>@next begin set @[email protected][email protected] set @location=charindex(@split,@str,@start) set @[email protected]+1 end if @location =0 select @location =len(@str)+1 --這兒存在兩種情況:1、字串不存在分隔符號 2、字串中存在分隔符號,跳出while迴圈後,@location為0,那預設為字串後邊有一個分隔符號。 return substring(@str,@start,@[email protected]) end ; ---測試。注意: P_GET_HttpRequestData 為sqlserver獲取http請求資料的儲存過程,在Sqlserver處理Http請求日誌中有講述。 CREATE PROCEDURE IPCC_IVR_QUERYCARDONECINFO(@dataStr VARCHAR(255),@dataIndex VARCHAR(4),@outText1 VARCHAR(255) OUT,@outText2 VARCHAR(255) OUT,@outText3 VARCHAR(255) OUT) AS BEGIN DECLARE @returnText VARCHAR(500) DECLARE @status int DECLARE @urlStr VARCHAR(255) SET @urlStr = 'http://172.17.5.14:9090/df/sendDataToIVR.action?parametersStr='[email protected] +'#'[email protected]+'&ifaceType=2'; EXEC P_GET_HttpRequestData @urlStr, @status OUTPUT, @returnText OUTPUT; SET @outText1 = dbo.Get_StrArrayStrOfIndex(@returnText,',',1); SET @outText2 = dbo.Get_StrArrayStrOfIndex(@returnText,',',2); SET @outText3 = dbo.Get_StrArrayStrOfIndex(@returnText,',',3); END;