如何在SQL Server中的已知字符之间剪切子字符串

时间:2014-10-10 12:57:06

标签: sql-server tsql

如何在SQL Server中的两个已知字符序列之间剪切子字符串?

例如:这是我的字符串(表格中的列)

  

' DateTimeFormat = dd.MM.yyyy& ReportDate_FromDate = 08/11/2014 00:00:00& ReportDate_ToDate = 08/12/2014 23:59:00& Reports_Brand:isnull = true& Reports_Portal:isnull =真安培; Reports_Currency:ISNULL =真安培; ReportBy = WEEK&安培;排序依据:ISNULL =真安培; IncludeDataForLastHour:ISNULL =真安培;服务器名称:ISNULL =真安培;用户= pirman1&安培; Reports_Export =假&安培;内'

我希望仅查看pirman1&User=之间的&

2 个答案:

答案 0 :(得分:0)

substringcharindex的组合应该可以解决问题:

SELECT SUBSTR(str_col, start_pos, end_pos - start_pos + 1)
FROM   (SELECT str_col,
               CHARINDEX('&User=', str_col) AS start_pos, 
               CHARINDEX('&', str_col, CHARINDEX('&User=', str_col) + 1) AS end_pos
        FROM   my_table) t

答案 1 :(得分:0)

我已经测试了这段代码,对Mureinik的代码略有改动,这个代码工作得很好。

 SELECT REPLACE(REPLACE(SUBSTRING(string, start_pos,  end_pos - start_pos  
    +1), '&User=', ''), '&', '')

   FROM   (SELECT string,
           CHARINDEX('&User=', string) AS start_pos, 
           CHARINDEX('&', string, CHARINDEX('&User=', string)+1)  AS end_pos
           FROM dbo.TestTbl
      ) AS abc