I've found a solution for finding the position of an underscore with PATINDEX :
DECLARE @a VARCHAR(10) SET @a = '37_21' PRINT PATINDEX('%_%', @a) -- return 1 (false) PRINT PATINDEX('%!%', REPLACE(@a, '_', '!')) -- return 3 (correct)
Have you other ideas? Like a way to escape the underscore character?
The PATINDEX() function in the SQL server is used to return the starting index of the first occurrence of a pattern in a string or a specified expression. It returns zero if the pattern is not found. It returns NULL if either pattern or expression is NULL.
SQL Server PATINDEX() Function The PATINDEX() function returns the position of a pattern in a string. If the pattern is not found, this function returns 0. Note: The search is case-insensitive and the first position in string is 1.
In SQL Server (Transact-SQL), the PATINDEX functions returns the location of a pattern in a string. The search is not case-sensitive.
The PATINDEX() function returns the position of the first occurrence of a pattern in a string.
I've always done it with brackets: '%[_]%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With