我正在使用PATINDEX进行一些字符串操作,以修复XML中一些不正确的时间格式,例如(2018-12-20T17:00:00-05:00)。
我遇到的问题是,PATINDEX在@IncorrectMatchIndex字符串中找到了一个与@模式匹配的模式。
您可以通过运行以下命令重新创建此问题:
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发布于 2018-12-13 16:45:07
我猜想你是想:
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>%'然后返回0 for IncorrectMatchIndex。
当然,注释是正确的,您应该为此使用XQUERY。但是,我不能为此提供一个示例,因为您提供的任何XML数据都没有有效的XML (例如,@CorrectMatchIndex以'</Rate>'结尾,但该节点从未打开)。
发布于 2018-12-13 16:41:19
据我所知,@IncorrectMatchIndex字符串不包含与
%<EstmatedTime>%T%-%</EstmatedTime>%匹配的内容。在T和关闭</EstmatedTime>之间没有破折号
是的有。因为字符串后面有第二组<EstimatedTime>标记,而且在第一个T和最后一个</EstimatedTime>之间肯定有一个'-‘字符。
https://stackoverflow.com/questions/53766190
复制相似问题