Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape underscore character in PATINDEX pattern argument?

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?

like image 515
podosta Avatar asked May 14 '09 14:05

podosta


People also ask

What is Patindex with an example write SQL query and result?

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.

What does Patindex do in SQL Server?

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.

Is Patindex case sensitive?

In SQL Server (Transact-SQL), the PATINDEX functions returns the location of a pattern in a string. The search is not case-sensitive.

Which of the following SQL function is used to find the starting location of a pattern in a string?

The PATINDEX() function returns the position of the first occurrence of a pattern in a string.


1 Answers

I've always done it with brackets: '%[_]%'

like image 63
Curt Hagenlocher Avatar answered Sep 24 '22 07:09

Curt Hagenlocher