SQL で文字列をコンマで分割し、それらを split_item1、split_item2、および
select column_name from table-name where column_name = 'split_item1' or split_item2'
これの正しい構文を投稿してください
select * fromSplitStringSep('Word1 wordr2 word3',' ')
CREATE function [dbo].[SplitStringSep]
(
@str nvarchar(4000),
@separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
select
1,
1,
charindex(@separator, @str)
union all
select
p + 1,
b + 1,
charindex(@separator, @str, b + 1)
from tokens
where b > 0
)
select
p-1 zeroBasedOccurance,
substring(
@str,
a,
case when b > 0 then b-a ELSE 4000 end)
AS s
from tokens
)
次のコードを使用できます (sqlserver 用に作成)。
Create Function FUN_STRING_SPLIT(@text varchar(max), @separator varchar(10))
Returns @Result Table
(
POSITION INT IDENTITY(1,1) NOT NULL, VALUE VARCHAR(max)
)
As
Begin
Declare @ini int = 1, @end int = 0
While @end <= LEN(@text)
Begin
if @end > 0 set @ini = @end + LEN(@separator)
if @ini > 0 set @end = CHARINDEX(@separator, @text, @ini)
if @end = 0 set @end = LEN(@text) + 1
Insert Into @Result (VALUE) Values (SUBSTRING(@text,@ini,@end - @ini))
End
Return
End
次に、次のように使用します。
Select * From FUN_STRING_SPLIT('Word1,Word2,Word2,Etc..', ',');