Sql server 2008 r2 与数据替换相关的SQL Server 2008 R2查询

Sql server 2008 r2 与数据替换相关的SQL Server 2008 R2查询,sql-server-2008-r2,Sql Server 2008 R2,我有一个场景,其中我必须删除除a、b或c之外的所有字符串 我的样本表如下: Id Product ------------------ 1. a,b,Da,c 2. Ty,a,b,c 3. a,sds,b 样本输出 Id Product ---------------- 1. a,b,c 2. a,b,c 3. a,b 我当前的版本是Microsoft SQL Server 2008 R2这应该可以帮助您解决问题。正如我在评论中所

我有一个场景,其中我必须删除除a、b或c之外的所有字符串

我的样本表如下:

Id     Product
------------------
1.     a,b,Da,c
2.     Ty,a,b,c
3.     a,sds,b
样本输出

Id    Product
----------------
1.     a,b,c
2.     a,b,c
3.     a,b

我当前的版本是Microsoft SQL Server 2008 R2

这应该可以帮助您解决问题。正如我在评论中所说的,我使用Jeff Moden的,因为您使用的是旧版本的SQL Server。如果您使用的是2016+,您将有权访问STRING_SPLIT。我也将你的数据标准化;因为存储分隔数据几乎总是一个坏主意

CREATE TABLE #Sample (id int, Product varchar(20));

INSERT INTO #Sample
VALUES (1,'a,b,Da,c'),
       (2,'Ty,a,b,c'),
       (3,'a,sds,b');
GO

--The first problem you have is you're storing delimited data
--You really should be storing each item on a separate row.
--This is, however, quite easy to do. i'm going to use a different
--table, however, you can change this fairly easily for your
--needs.

CREATE TABLE #Sample2  (id int, Product varchar(2));

GO
--You can split the data out by using a Splitter.
--My personal preference is Jeff Moden's DelimitedSplit8K
--which I've linked to above.
INSERT INTO #Sample2 (id, Product)
SELECT id, Item AS Product
FROM #Sample S
     CROSS APPLY dbo.DelimitedSplit8K(S.Product,',') DS
WHERE DS.Item IN ('a','b','c');
GO
--And hey presto! Your normalised data, and without the unwanted values
SELECT *
FROM #Sample2;

GO

DROP TABLE #Sample;
DROP TABLE #Sample2;
如果必须保留分隔格式,可以使用STUFF和FOR XML PATH:


这也可以做到这一点,只使用xml:

select * into #t from (values('a,b,Da,c'),('Ty,a,b,c'),('a,sds,b'))v(Product)
;
with  x as (
    SELECT t.Product, st.sProduct
    FROM #t t
        cross apply (
            SELECT CAST(N'<root><r>' + REPLACE(t.Product,',', N'</r><r>') + N'</r></root>' as xml) xProduct
        )xt
        cross apply (
            select CAST(r.value('.','NVARCHAR(MAX)') as nvarchar) sProduct
            from xt.xProduct.nodes(N'//root/r') AS RECORDS(r)
        ) st
    where st.sProduct in ('a', 'b', 'c')
)
select distinct x.Product, REVERSE(SUBSTRING(REVERSE(cleared.cProduct), 2, 999)) cleared
from x
    cross apply ( select (
        select distinct ref.sProduct + ','
        from x ref
        where ref.Product = x.Product
        for xml path('') )
    )cleared(cProduct)
;
drop table #t

那么,您在这里的预期输出是什么?SQL Server的哪个版本,最重要的是存储在单个列中的分隔数据;如果是这样的话,为什么?是的,我把它存储在一列中,因为它们都在一个共同的标签下。你说用替换它,但是,你的输出并不代表这一点。例如,我希望a,b,Da,c变成a,b,,c,如果你用一个空字符串替换它,你是说要删除这些元素吗?好的,但是存储分隔数据仍然是个坏主意。另外,您还没有标记您的版本。请这样做;这与答案大不相同。你的版本是。。?我只能问这么多次。谢谢。嘿,谢谢你的回复…但我希望它在同一个单元格中,因为我有一个基于另一个条件将其正常化的场景…所以你能告诉我是否可以将其作为分隔值放在同一个单元格中…谢谢advance@vamseedharankalyanakrishnan还添加了如何创建分隔列表;但是,我仍然建议您规范化数据。Heyyy…还有其他方法不使用DelimitedSplit8k…只是好奇这里是,是的;不过我建议你自己找一找。询问如何在SQLServer中拆分分隔列表可能是最常见的问题之一。如果你自己做研究,你会学到更多。
select * into #t from (values('a,b,Da,c'),('Ty,a,b,c'),('a,sds,b'))v(Product)
;
with  x as (
    SELECT t.Product, st.sProduct
    FROM #t t
        cross apply (
            SELECT CAST(N'<root><r>' + REPLACE(t.Product,',', N'</r><r>') + N'</r></root>' as xml) xProduct
        )xt
        cross apply (
            select CAST(r.value('.','NVARCHAR(MAX)') as nvarchar) sProduct
            from xt.xProduct.nodes(N'//root/r') AS RECORDS(r)
        ) st
    where st.sProduct in ('a', 'b', 'c')
)
select distinct x.Product, REVERSE(SUBSTRING(REVERSE(cleared.cProduct), 2, 999)) cleared
from x
    cross apply ( select (
        select distinct ref.sProduct + ','
        from x ref
        where ref.Product = x.Product
        for xml path('') )
    )cleared(cProduct)
;
drop table #t