PATINDEX 的意外结果
Unexpected results with PATINDEX
我正在使用 PATINDEX 进行一些字符串操作,以修复 XML 中一些不正确的时间格式,例如(2018-12-20T17:00:00-05:00).
我遇到的问题是 PATINDEX 正在@IncorrectMatchIndex 字符串中找到与@Pattern 的匹配项。
您可以通过运行以下方式重新创建问题:
DECLARE @Pattern nvarchar(36) = '%<EstmatedTime>%T%-%</EstmatedTime>%',
@CorrectMatchIndex nvarchar(100) = '<DiscountedRate>263.34</DiscountedRate><EstmatedTime>2018-12-20T17:00:00-05:00</EstmatedTime></Rate>',
@CorrectMatchIndex2 nvarchar(94) = '<DiscountedRate>263.34</DiscountedRate><EstmatedTime>2018-12-20T17:00:00</EstmatedTime></Rate>',
@IncorrectMatchIndex nvarchar(296) = '<DiscountedRate>263.34</DiscountedRate><EstmatedTime>2018-12-20T17:00:00</EstmatedTime></Rate><Rate><Carrier>FedEx Freight</Carrier><Service>FEDEX_FREIGHT_PRIORITY</Service><PublishedRate>520.6</PublishedRate><DiscountedRate>272.04</DiscountedRate><EstmatedTime>2018-12-18T17:00:00</EstmatedTime>'
SELECT
PATINDEX(@Pattern, @CorrectMatchIndex) AS CorrectMatchIndex,
PATINDEX(@Pattern, @CorrectMatchIndex2) AS CorrectMatchIndex2,
PATINDEX(@Pattern, @IncorrectMatchIndex) AS IncorrectMatchIndex
The @IncorrectMatchIndex string does not contain a match to %<EstmatedTime>%T%-%</EstmatedTime>%
as far as I can see. There is no dash between the T and closing </EstmatedTime>
是的。因为在字符串后面还有第二组 <EstimatedTime>
标记,并且肯定在 first T
和 [= 之间有一个 '-' 字符18=]最后 </EstimatedTime>
纯属猜测,我怀疑您想要:
DECLARE @Pattern nvarchar(300) = '%<EstmatedTime>[1-2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]</EstmatedTime>%'
然后 returns 0
for IncorrectMatchIndex
.
当然,评论是对的,你真的应该为此使用 XQUERY。但是,我无法为此提供示例,因为您提供的 XML 数据中的 none 有效 XML (例如 @CorrectMatchIndex
以 '</Rate>'
但该节点从未打开过)。
我正在使用 PATINDEX 进行一些字符串操作,以修复 XML 中一些不正确的时间格式,例如(2018-12-20T17:00:00-05:00).
我遇到的问题是 PATINDEX 正在@IncorrectMatchIndex 字符串中找到与@Pattern 的匹配项。
您可以通过运行以下方式重新创建问题:
DECLARE @Pattern nvarchar(36) = '%<EstmatedTime>%T%-%</EstmatedTime>%',
@CorrectMatchIndex nvarchar(100) = '<DiscountedRate>263.34</DiscountedRate><EstmatedTime>2018-12-20T17:00:00-05:00</EstmatedTime></Rate>',
@CorrectMatchIndex2 nvarchar(94) = '<DiscountedRate>263.34</DiscountedRate><EstmatedTime>2018-12-20T17:00:00</EstmatedTime></Rate>',
@IncorrectMatchIndex nvarchar(296) = '<DiscountedRate>263.34</DiscountedRate><EstmatedTime>2018-12-20T17:00:00</EstmatedTime></Rate><Rate><Carrier>FedEx Freight</Carrier><Service>FEDEX_FREIGHT_PRIORITY</Service><PublishedRate>520.6</PublishedRate><DiscountedRate>272.04</DiscountedRate><EstmatedTime>2018-12-18T17:00:00</EstmatedTime>'
SELECT
PATINDEX(@Pattern, @CorrectMatchIndex) AS CorrectMatchIndex,
PATINDEX(@Pattern, @CorrectMatchIndex2) AS CorrectMatchIndex2,
PATINDEX(@Pattern, @IncorrectMatchIndex) AS IncorrectMatchIndex
The @IncorrectMatchIndex string does not contain a match to
%<EstmatedTime>%T%-%</EstmatedTime>%
as far as I can see. There is no dash between the T and closing</EstmatedTime>
是的。因为在字符串后面还有第二组 <EstimatedTime>
标记,并且肯定在 first T
和 [= 之间有一个 '-' 字符18=]最后 </EstimatedTime>
纯属猜测,我怀疑您想要:
DECLARE @Pattern nvarchar(300) = '%<EstmatedTime>[1-2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]</EstmatedTime>%'
然后 returns 0
for IncorrectMatchIndex
.
当然,评论是对的,你真的应该为此使用 XQUERY。但是,我无法为此提供示例,因为您提供的 XML 数据中的 none 有效 XML (例如 @CorrectMatchIndex
以 '</Rate>'
但该节点从未打开过)。