Sql server 2012 SQL Server:使用单引号拆分字符串值

Sql server 2012 SQL Server:使用单引号拆分字符串值,sql-server-2012,Sql Server 2012,试图修改一组代码,以便给定一个字符串,必须拆分该字符串并将其传递给要使用的代码 这是我现在掌握的代码 DECLARE @xml xml, @str varchar(100), @delimiter varchar(10) SET @str = '100' SET @delimiter = ',' SET @xml = cast(('<X>'+replace(@str, @delimiter, '</X><X>')+'<

试图修改一组代码,以便给定一个字符串,必须拆分该字符串并将其传递给要使用的代码

这是我现在掌握的代码

DECLARE @xml xml, 
        @str varchar(100), 
        @delimiter varchar(10)
SET @str = '100' 
SET @delimiter = ','
SET @xml = cast(('<X>'+replace(@str, @delimiter, '</X><X>')+'</X>') as 
xml)
SELECT C.value('.', 'varchar(10)') as value 
FROM @xml.nodes('X') as X(C)

如何修改此要求的代码?

您需要创建一个表值函数,您可以使用
交叉应用将字符串拆分为:

作用
我的帐户没有创建函数的权限。有没有办法在不创建函数的情况下执行此操作?您可以使用CTEs将函数包含在查询中@LucasC922(并用相应的列名/字符串文字替换变量)
declare @values table
(
Value varchar(1000)
)

insert into @values values ('100'),('100A'),('100B'),('100C')

Select *
from table
where myField in (select value from @value)
create function [dbo].[fn_StringSplit4k]
(
     @str nvarchar(4000) = ' '              -- String to split.
    ,@delimiter as nvarchar(20) = ','       -- Delimiting value to split on.
    ,@num as int = null                     -- Which value to return.
)
returns table
as
return
                    -- Start tally table with 10 rows.
    with n(n)   as (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1)

                    -- Select the same number of rows as characters in @str as incremental row numbers.
                    -- Cross joins increase exponentially to a max possible 10,000 rows to cover largest @str length.
        ,t(t)   as (select top (select len(isnull(@str,'')) a) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4)

                    -- Return the position of every value that follows the specified delimiter.
        --,s(s)   as (select 1 union all select t+len(replace(@delimiter,' ','.')) from t where substring(isnull(@str,''),t,len(replace(@delimiter,' ','.'))) = @delimiter)
        ,s(s)   as (select 1 union all select t+1 from t where case when @delimiter = '' and t < len(@str) then 1 else case when substring(isnull(@str,''),t,1) = @delimiter then 1 else 0 end end = 1)

                    -- Return the start and length of every value, to use in the SUBSTRING function.
                    -- ISNULL/NULLIF combo handles the last value where there is no delimiter at the end of the string.
        ,l(s,l) as (select s,case when @delimiter = '' then 1 else isnull(nullif(charindex(@delimiter,isnull(@str,''),s),0)-s,4000) end from s)

    select rn
          ,item
    from(select row_number() over(order by s) as rn
                ,substring(@str,s,l) as item
        from l
        ) a
    where rn = @num
        or @num is null;
select s.item
from YourTable as t
    cross apply dbo.fn_StringSplit4k(t.YourString,',',null) as s;