Sql 您提供的样本元素。谢谢!!这就是它,除了最后一个+25属于第二个CHARINDEX(如果我没有弄错的话)应该是-25。我也做过类似的事情,但我没有得到正确的答案,我想我只是放弃了我的想法,不得不问。过了一会儿,一切开始变得混乱起来。非常感谢你!!还有其他人
Sql 您提供的样本元素。谢谢!!这就是它,除了最后一个+25属于第二个CHARINDEX(如果我没有弄错的话)应该是-25。我也做过类似的事情,但我没有得到正确的答案,我想我只是放弃了我的想法,不得不问。过了一会儿,一切开始变得混乱起来。非常感谢你!!还有其他人,sql,sql-server,substring,charindex,Sql,Sql Server,Substring,Charindex,您提供的样本元素。谢谢!!这就是它,除了最后一个+25属于第二个CHARINDEX(如果我没有弄错的话)应该是-25。我也做过类似的事情,但我没有得到正确的答案,我想我只是放弃了我的想法,不得不问。过了一会儿,一切开始变得混乱起来。非常感谢你!!还有其他人!你能帮我更好地理解语法吗?我被困在第二和第三个charindex中,试图了解它到底是如何工作的 <SendDocument DocumentID="1234567">true</SendDocument> SELEC
您提供的样本元素。谢谢!!这就是它,除了最后一个+25属于第二个CHARINDEX(如果我没有弄错的话)应该是-25。我也做过类似的事情,但我没有得到正确的答案,我想我只是放弃了我的想法,不得不问。过了一会儿,一切开始变得混乱起来。非常感谢你!!还有其他人!你能帮我更好地理解语法吗?我被困在第二和第三个charindex中,试图了解它到底是如何工作的
<SendDocument DocumentID="1234567">true</SendDocument>
SELECT SUBSTRING(xml_column, CHARINDEX('>true</SendDocument>', xml_column) - CHARINDEX('<SendDocument',xml_column) +10087,9)
SELECT SUBSTRING(cip_msg, CHARINDEX('<SendDocument',cip_msg)+26,7)
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)
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
CHARINDEX('<SendDocument DocumentID=', xml_column) + 25
((adding 25 because I think CHARINDEX gives you the position at the beginning of the string you are searching for))
CHARINDEX('>true</SendDocument>',xml_column) - CHARINDEX('<SendDocument DocumentID=', xml_column)+25
((Position of the ending text minus the position of the start text))
SELECT SUBSTRING(xml_column, CHARINDEX('<SendDocument DocumentID=', xml_column)+25,(CHARINDEX('>true</SendDocument>',xml_column) - CHARINDEX('<SendDocument DocumentID=', xml_column)+25))