分裂字符串中的某些字符问题

时间:2015-01-15 14:11:33

标签: sql sql-server sql-server-2008

以下是存储过程

;WITH cte AS (
SELECT 
    AgentId,
    CAST('<r>' + REPLACE(States, ',', '</r><r>') + '</r>' AS XML) AS States,
    CAST('<r>' + REPLACE(REPLACE(Products,'&','&amp;'), ',', '</r><r>') + '</r>' AS XML) AS Products
FROM @tbVendor
 )
  ,FinalList AS (
SELECT 
AgentId,
RTRIM(LTRIM (sTable.sColumn.value('.', 'VARCHAR(MAX)'))) AS States,
RTRIM(LTRIM (PTable.PColumn.value('.', 'VARCHAR(MAX)'))) AS Products
FROM cte
CROSS APPLY States.nodes('//r') AS sTable(sColumn) 
CROSS APPLY Products.nodes('//r') AS PTable(PColumn) 
)
SELECT DISTINCT F.Products AS ProductName
   ,T.ProductId AS ProductId       
FROM FinalList F
CROSS APPLY (SELECT ProductId FROM @tbProduct TP WHERE TP.ProductName = F.Products) AS          T
 WHERE F.States = 'New York'
 AND F.AgentId = 1
 ORDER BY T.ProductId ASC

这是SQL小提琴

http://rextester.com/SVXKFH57654

它工作得很好而且很完美,但它消除了&#34; - &#34; ProductName feild中的字符,例如非Stick Utensils ...等

我无法解决这个问题......请帮助我!!!

1 个答案:

答案 0 :(得分:0)

对于拆分字符串,您使用XML。在这种情况下,您将遇到一些字符问题。例如&amp;,&lt;,&gt;。您可以通过使用另一种拆分字符串

的方法来避免这种情况

表函数拆分字符串:

CREATE FUNCTION [dbo].[SplitStr] (
    @str varchar(MAX)
    ,@sep char(1)=','
)
RETURNS TABLE 
AS
RETURN 
(
    WITH Split ( n1, n2) 
    AS 
    ( 
    SELECT CAST(0 as bigint) as n1, CHARINDEX(@sep, @str + @sep) as n2
    UNION ALL 
    SELECT n2 as n1, CHARINDEX(@sep, @str + @sep, n2 + 1) as n2
    FROM Split
    WHERE n2 < LEN(@str)
    )

    SELECT SUBSTRING(@str, n1+1, n2-n1-1) as Col FROM Split
)
GO

使用此功能:

SELECT
    tbVendor.AgentId
    ,States.Col as States
    ,Products.Col as Products
FROM @tbVendor as tbVendor
    CROSS APPLY [dbo].[SplitStr](States, ',') as States
    CROSS APPLY [dbo].[SplitStr](Products, ',') as Products

这相当于你的代码

-- First convert all comma separated data into tabular form as:
;WITH cte AS (
    SELECT 
        AgentId,
        CAST('<r>' + REPLACE(States, ',', '</r><r>') + '</r>' AS XML) AS States,
        CAST('<r>' + REPLACE(Products, ',', '</r><r>') + '</r>' AS XML) AS Products
    FROM @tbVendor
)
SELECT 
    AgentId,
    sTable.sColumn.value('.', 'VARCHAR(MAX)') AS States,
    PTable.PColumn.value('.', 'VARCHAR(MAX)') AS Products
FROM cte
CROSS APPLY States.nodes('//r') AS sTable(sColumn) 
CROSS APPLY Products.nodes('//r') AS PTable(PColumn)