使用SQL将重复的特殊字符拆分为查询字符串

时间:2013-12-30 05:48:57

标签: sql sql-server

这是我的字符串

Declare @qstr as varchar(max)='hireteammember.aspx?empemail=kuldeep@asselsolutions.com&empid=376&empname=kuldeep&adminname=TMA1&term=5&teamid=161&contactid=614¥1&WP=100¥5¥Months&Amt=500&DueDay=5&StrDt=12/31/2013&MemCatg=Employees&StrTm=21:05&PlnHrs=5&WrkDays=true¥true¥true¥true¥true¥false¥false'

我想提取empid,empname,adminname,term,teamid,contactid,WP,Months,Dueday,StrDt,MemCatgmStrTm,PlnHrs,WrkDays的值并将它们分配给新变量

我用过

select ( SUBSTRING(@qstr,CHARINDEX('=',@qstr)+1,CHARINDEX('&',@qstr)-CHARINDEX('=',@qstr)-1)))

但只获得'empemail',以便下次出现特殊字符'&' ,如果我使用'&',则无法获得更多条款的值尽管'='。

帮我拆分整个字符串

4 个答案:

答案 0 :(得分:3)

如何使用XML将值拆分为行,然后将它们拆分为列。

这样的东西
Declare @qstr as varchar(max)='hireteammember.aspx?empemail=kuldeep@asselsolutions.com&empid=376&empname=kuldeep&adminname=TMA1&term=5&teamid=161&contactid=614¥1&WP=100¥5¥Months&Amt=500&DueDay=5&StrDt=12/31/2013&MemCatg=Employees&StrTm=21:05&PlnHrs=5&WrkDays=true¥true¥true¥true¥true¥false¥false'

DECLARe @str VARCHAR(MAX) = SUBSTRING(@qstr,CHARINDEX('?',@qstr,0) + 1, LEN(@qstr)-CHARINDEX('?',@qstr,0))
DECLARE @xml XML
SELECT  @xml = CAST('<d>' + REPLACE(@str, '&', '</d><d>') + '</d>' AS XML)

;WITH Vals AS (
        SELECT  T.split.value('.', 'nvarchar(max)') AS data
        FROM    @xml.nodes('/d') T(split)
)
SELECT  LEFT(data,CHARINDEX('=',data,0) - 1),
        RIGHT(data,LEN(data) - CHARINDEX('=',data,0))
FROM    Vals

SQL Fiddle DEMO

答案 1 :(得分:1)

CREATE FUNCTION dbo.SplitQueryString (@s varchar(8000))
RETURNS table
AS
RETURN (
    WITH splitter_cte AS (
      SELECT CHARINDEX('&', @s) as pos, 0 as lastPos
      UNION ALL
      SELECT CHARINDEX('&', @s, pos + 1), pos
      FROM splitter_cte
      WHERE pos > 0
      ),
    pair_cte AS (
    SELECT chunk,
           CHARINDEX('=', chunk) as pos
    FROM (
        SELECT SUBSTRING(@s, lastPos + 1,
                         case when pos = 0 then 80000
                         else pos - lastPos -1 end) as chunk
        FROM splitter_cte) as t1
  )
    SELECT substring(chunk, 0, pos) as keyName,
           substring(chunk, pos+1, 8000) as keyValue
    FROM pair_cte
)
GO

declare @queryString varchar(2048)
set @queryString = 'foo=bar&temp=baz&key=value';
SELECT *
  FROM dbo.SplitQueryString(@queryString)
OPTION(MAXRECURSION 0);

运行时产生以下输出。

keyName  keyValue

-------  --------

foo      bar

temp     baz

key      value

(3 row(s) affected)

我相信这会完全符合您的要求。

SQL FIDDLE DEMO

答案 2 :(得分:0)

如果html字符串中的值的顺序保持不变,我建议使用整个字符串名称,如

select ( SUBSTRING(@qstr,CHARINDEX('empemail=',@qstr)+1,CHARINDEX('&empid=',@qstr)-CHARINDEX('empemail=',@qstr)-1)))

如果您仍在寻找第n次出现,请参阅this link

答案 3 :(得分:0)

    Declare @qstr as varchar(max)='hireteammember.aspx?empemail=kuldeep@asselsolutions.com&empid=376&empname=kuldeep&adminname=TMA1&term=5&teamid=161&contactid=614¥1&WP=100¥5¥Months&Amt=500&DueDay=5&StrDt=12/31/2013&MemCatg=Employees&StrTm=21:05&PlnHrs=5&WrkDays=true¥true¥true¥true¥true¥false¥false'

(select ( SUBSTRING(@qstr,CHARINDEX('&empname=',@qstr)+1,CHARINDEX('&adminname=',@qstr)-CHARINDEX('&empname=',@qstr)-1)))

(select ( SUBSTRING(@qstr,CHARINDEX('?empemail=',@qstr)+1,CHARINDEX('&empid=',@qstr)-CHARINDEX('?empemail=',@qstr)-1)))   
像这样 我已经拆分并更新了整个字符串。谢谢大家的答案,你的答案帮我解决了这个问题