tsql - Is it possible to match any character that is not ']' in PATINDEX? -
i need find index of first character not ]. match character except x, use pattern [^x]. problem [^]] closes first bracket early. first part, [^], match character.
in documentation like operator, if scroll down section "using wildcard characters literals" shows table of methods indicated literal characters [ , ] inside pattern. makes no mention of using [ or ] inside double brackets. if pattern being used like operator, use escape clause. like doesn't return index , patindex doesn't seem have parameter escape clause.
is there no way this?
(this may seem arbitrary. put context around it, need match ] followed character not ] in order locate end of quoted identifier. ]] character escape inside quoted identifier.)
this isn't possible. connect item patindex missing escape clause closed won't fix.
i'd use clr , regular expressions.
a simple implementation might be
using system.data.sqltypes; using system.text.regularexpressions; public partial class userdefinedfunctions { [microsoft.sqlserver.server.sqlfunction] public static sqlint32 patindexclr(sqlstring pattern, sqlstring expression) { if (pattern.isnull || expression.isnull) return new sqlint32(); match match = regex.match(expression.tostring(), pattern.tostring()); if (match.success) { return new sqlint32(match.index + 1); } else { return new sqlint32(0); } } } with example usage
select [dbo].[patindexclr] ( n'[^]]', n']]]]]]]]abc[def'); if not option possible flaky workaround might substitute character unlikely in data without special significance in grammar.
with t(value) ( select ']]]]]]]]abc[def' ) select patindex('%[^' + char(7) + ']%', replace(value,']', char(7))) t (returns 9)
Comments
Post a Comment