當前位置:首頁 » 編程語言 » sql分隔符split
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql分隔符split

發布時間: 2023-06-26 23:22:04

sql資料庫字元串按照『|』分開

--方法0:動態SQL法declare @s varchar(100),@sql varchar(1000)set @s='1,2,3,4,5,6,7,8,9,10'set @sql='select col='''+ replace(@s,',',''' union all select ''')+''''PRINT @sqlexec (@sql)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_splitSTR]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[f_splitSTR]GO--方法1:循環截取法CREATE FUNCTION f_splitSTR(@s varchar(8000), --待分拆的字元串@split varchar(10) --數據分隔符)RETURNS @re TABLE(col varchar(100))ASBEGIN DECLARE @splitlen int SET @splitlen=LEN(@split+'a')-2 WHILE CHARINDEX(@split,@s)>0 BEGIN INSERT @re VALUES(LEFT(@s,CHARINDEX(@split,@s)-1)) SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'') END INSERT @re VALUES(@s) RETURNENDGO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_splitSTR]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[f_splitSTR]GO--方法2:使用臨時性分拆輔助表法CREATE FUNCTION f_splitSTR(@s varchar(8000), --待分拆的字元串@split varchar(10) --數據分隔符)RETURNS @re TABLE(col varchar(100))ASBEGIN --創建分拆處理的輔助表(用戶定義函數中只能操作表變數) DECLARE @t TABLE(ID int IDENTITY,b bit) INSERT @t(b) SELECT TOP 8000 0 FROM syscolumns a,syscolumns b
INSERT @re SELECT SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID) FROM @t WHERE ID<=LEN(@s+'a') AND CHARINDEX(@split,@split+@s,ID)=ID RETURNENDGO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_splitSTR]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[f_splitSTR]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tb_splitSTR]') and objectproperty(id,N'IsUserTable')=1)drop table [dbo].[tb_splitSTR]GO--方法3:使用永久性分拆輔助表法--字元串分拆輔助表SELECT TOP 8000 ID=IDENTITY(int,1,1) INTO dbo.tb_splitSTRFROM syscolumns a,syscolumns bGO--字元串分拆處理函數CREATE FUNCTION f_splitSTR(@s varchar(8000), --待分拆的字元串@split varchar(10) --數據分隔符)RETURNS TABLEASRETURN( SELECT col=CAST(SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID) as varchar(100)) FROM tb_splitSTR WHERE ID<=LEN(@s+'a') AND CHARINDEX(@split,@split+@s,ID)=ID)GO

⑵ oracle 如何用sql實現split功能

本函數可以將「目標字元串」以「指定字元串」進行拆分,並通過表結構返回結果。代碼如下:

CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN str_split
PIPELINED
AS
v_length NUMBER := LENGTH(p_string);
v_start NUMBER := 1;
v_index NUMBER;
BEGIN
WHILE(v_start <= v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);

IF v_index = 0
THEN
PIPE ROW(SUBSTR(p_string, v_start));
v_start := v_length + 1;
ELSE
PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
v_start := v_index + 1;
END IF;
END LOOP;

RETURN;
END splitstr;

創建完畢後,我們來測試一下,例如執行如下SQL:

select * from table(splitstr('Hello,Cnblogs!',','));

其輸出結果為一個兩行的表,如下圖:

⑶ 在SQL中,如何將一個用冒號分隔開的值,分隔成一個數組!!

表值函數實現Split方法

1 Create FUNCTION [dbo].[SplitToTable]
2 (
3 @SplitString nvarchar(max),
4 @Separator nvarchar(10)=' '
5 )
6 RETURNS @SplitStringsTable TABLE
7 (
8 [id] int identity(1,1),
9 [value] nvarchar(max)
10 )
11 AS
12 BEGIN
13 DECLARE @CurrentIndex int;
14 DECLARE @NextIndex int;
15 DECLARE @ReturnText nvarchar(max);
16 SELECT @CurrentIndex=1;
17 WHILE(@CurrentIndex<=len(@SplitString))
18 BEGIN
19 SELECT @NextIndex=charindex(@Separator,@SplitString,@CurrentIndex);
20 IF(@NextIndex=0 OR @NextIndex IS NULL)
21 SELECT @NextIndex=len(@SplitString)+1;
22 SELECT @ReturnText=substring(@SplitString,@CurrentIndex,@NextIndex-@CurrentIndex);
23 INSERT INTO @SplitStringsTable([value]) VALUES(@ReturnText);
24 SELECT @CurrentIndex=@NextIndex+1;
25 END
26 RETURN;
27 END

select * FROm dbo.SplitToTable('111:b2222:323232:32d:e:323232f:g3222', ':')

⑷ Split(SQL)

Split()是將字元串分割成數組;
sql是不是一個函數或者語句?如果向樓上說的是個函數的話應該是Split(SQL())才對啊
樓主說的不是很清楚,SQL沒有單獨出現的;
如果是個範例說明,SQL代表一個查詢的結果,那一個是查詢出來的分割的代表,而非SQL本身分成若干份:
做個範例樓主應該就明白了:

var str="a1-a2-a3-a4-a5";//假設有這樣一個字元串;

sql的執行結果是"-";
var a=str.Split("-");

那麼a這個數組應該是:
a[0]=a1;
a[1]=a2;
a[2]=a3;
a[3]=a4;
a[4]=a5;

如果SQL的運行結果是"-a";
var a=str.Split("-a");

那麼a這個數組應該是:
a[0]=a1
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;

⑸ sql 如何以逗號為分隔符分割一個欄位的值

可用substring函數。

創建測試表及數據:

createtabletest
(idvarchar(10));

insertintotestvalues('123abc');
insertintotestvalues('456def');
insertintotestvalues('789ghi');

執行:

selectsubstring(id,1,3)+','+substring(id,4,3)asidfromtest

結果截圖:

也就顯示成了用逗號分隔的樣子。

⑹ sql如何根據隔符分割字元串

  1. 資料庫自帶的substring()、charindex()函數,可以根據需要截取字元串,但並不能實現分割

  2. 自己寫分割函數,以下可以參考:

createfunctionGetStr
(
@strvarchar(1024),--要分割的字元串
@splitvarchar(10),--分隔符號
@indexint--取第幾個元素
)
returnsvarchar(1024)
as
begin
declare@locationint
declare@startint
declare@nextint
declare@seedint
set@str=ltrim(rtrim(@str))
set@start=1
set@next=1
set@seed=len(@split)

set@location=charindex(@split,@str)
while@location<>0and@index>@next
begin
set@start=@location+@seed
set@location=charindex(@split,@str,@start)
set@next=@next+1
end
if@location=0select@location=len(@str)+1
returnsubstring(@str,@start,@location-@start)
end

⑺ sql中的split

CREATE function [dbo].[split](@aString varchar(8000),@pattern varchar(10))
returns @temp table([Sid] [int] IDENTITY (1, 1) NOT NULL ,a varchar(100))
--實現split功能 的函數
--說明:@aString,寬拿字元串山戚,如a:b:c;@pattern,分隔標志,如慎唯搭 :
as
begin
declare @i int
set @aString=rtrim(ltrim(@aString))
set @i=charindex(@pattern,@aString)
while @i>=1
begin
insert @temp values(left(@aString,@i-1))
set @aString=substring(@aString,@i+1,len(@aString)-@i)
set @i=charindex(@pattern,@aString)
end
if @aString<>''
insert @temp values(@aString)
return
end

GO