您是否尝试过直接与XML类型的工作?如下图所示:
DECLARE @TempXmlTable TABLE
(XmlElement xml)
INSERT INTO @TempXmlTable
select Convert(xml,'<SendDocument DocumentID="1234567">true</SendDocument>')
SELECT
element.value('./@DocumentID', 'varchar(50)') as DocumentID
FROM
@TempXmlTable CROSS APPLY
XmlElement.nodes('//.') AS DocumentID(element)
WHERE element.value('./@DocumentID', 'varchar(50)') is not null
如果你只是想这个工作作为一个字符串,你可以做到以下几点:
DECLARE @SearchString varchar(max) = '<SendDocument DocumentID="1234567">true</SendDocument>'
DECLARE @Start int = (select CHARINDEX('DocumentID="',@SearchString)) + 12 -- 12 Character search pattern
DECLARE @End int = (select CHARINDEX('">', @SearchString)) - @Start --Find End Characters and subtract start position
SELECT SUBSTRING(@SearchString,@Start,@End)
下面是解析XML文档字符串的扩展版本。在下面的示例中,我创建了名为INSTR的PLSQL函数的副本,MS SQL数据库默认情况下不具有此功能。该功能将允许我在指定的起始位置搜索字符串。另外,我正在将一个示例XML字符串解析为一个变量临时表,并将它们视为符合我的搜索条件的行。这是因为可能有许多元素与单词DocumentID,我想要找到所有这些元素。见下:
IF EXISTS (select * from sys.objects where name = 'INSTR' and type = 'FN')
DROP FUNCTION [dbo].[INSTR]
GO
CREATE FUNCTION [dbo].[INSTR] (@String VARCHAR(8000), @SearchStr VARCHAR(255), @Start INT, @Occurrence INT)
RETURNS INT
AS
BEGIN
DECLARE @Found INT = @Occurrence,
@Position INT = @Start;
WHILE 1=1
BEGIN
-- Find the next occurrence
SET @Position = CHARINDEX(@SearchStr, @String, @Position);
-- Nothing found
IF @Position IS NULL OR @Position = 0
RETURN @Position;
-- The required occurrence found
IF @Found = 1
BREAK;
-- Prepare to find another one occurrence
SET @Found = @Found - 1;
SET @Position = @Position + 1;
END
RETURN @Position;
END
GO
--Assuming well formated xml
DECLARE @XmlStringDocument varchar(max) = '<SomeTag Attrib1="5">
<SendDocument DocumentID="1234567">true</SendDocument>
<SendDocument DocumentID="1234568">true</SendDocument>
</SomeTag>'
--Split Lines on this element tag
DECLARE @SplitOn nvarchar(25) = '</SendDocument>'
--Let's hold all lines in Temp variable table
DECLARE @XmlStringLines TABLE
(
Value nvarchar(100)
)
While (Charindex(@SplitOn,@XmlStringDocument)>0)
Begin
Insert Into @XmlStringLines (value)
Select
Value = ltrim(rtrim(Substring(@XmlStringDocument,1,Charindex(@SplitOn,@XmlStringDocument)-1)))
Set @XmlStringDocument = Substring(@XmlStringDocument,Charindex(@SplitOn,@XmlStringDocument)+len(@SplitOn),len(@XmlStringDocument))
End
Insert Into @XmlStringLines (Value)
Select Value = ltrim(rtrim(@XmlStringDocument))
--Now we have a table with multple lines find all Document IDs
SELECT
StartPosition = CHARINDEX('DocumentID="',Value) + 12,
--Now lets use the INSTR function to find the first instance of '">' after our search string
EndPosition = dbo.INSTR(Value,'">',(CHARINDEX('DocumentID="',Value)) + 12,1),
--Now that we know the start and end lets use substring
Value = SUBSTRING(value,(
-- Start Position
CHARINDEX('DocumentID="',Value)) + 12,
--End Position Minus Start Position
dbo.INSTR(Value,'">',(CHARINDEX('DocumentID="',Value)) + 12,1) - (CHARINDEX('DocumentID="',Value) + 12))
FROM
@XmlStringLines
WHERE Value like '%DocumentID%' --Only care about lines with a document id
该示例似乎是错误的...你能更具体吗? – NicoRiff
如果True这个单词后面跟着一串数字,那么你需要做的就是右(LEN(STRING) - POSITION OF TRUE + 1) –
@NicoRiff啊是的,不知道为什么它被切断了,这里又是一次... true SendDocument> –