原文地址:http://www.maomao365.com/?p=7307sql
--例: -----獲取下面字符串中中括號中的內容 -----咱們能夠將下列規則的字符串採用replace替換爲XML格式的字符串,而後採用xml select獲取相關信息 DECLARE @stringInfo VARCHAR(MAX) ='1:[貓貓小屋],2:[字符串分割],3:[sql教程博客]' , @q VARCHAR(3) =3; ---獲取第三個分割符中的信息 declare @xmlInfo xml ; set @stringInfo = '<xmlInfo keyId ="'+@stringInfo; set @stringInfo = REPLACE(@stringInfo, '[', '" value="'); set @stringInfo = REPLACE(@stringInfo, '],', '" /><xmlInfo keyId ="'); set @stringInfo = REPLACE(@stringInfo, ']', '" />'); set @stringInfo = REPLACE(@stringInfo, ':', ''); set @xmlInfo = CONVERT(xml,@stringInfo); SELECT @xmlInfo as [xmlInfo], @xmlInfo.value('(/xmlInfo[@keyId=sql:variable("@q")]/@value)[1]', 'varchar(60)');