Marwan
Marwan

Reputation: 1058

Unable to match square bracket [ character in SQL

I am trying to select records that start with this string pattern 'Dear [User Name]'. The following where clause:

where Message like 'Dear [UserName]%'

returns nothing.
What is the problem and what is the solution?

Note 1: the field [Message] DOES start with this text.
Note 2: the type of field [Message] is text, but the same problme happens with varchar fields.

Upvotes: 1

Views: 2587

Answers (2)

Vikram
Vikram

Reputation: 8333

try this

 where Message like 'Dear \[User Name\]' escape '\';

Upvotes: 2

Marwan
Marwan

Reputation: 1058

The [] is a wild card character matching operator which "Matches any single character within the specified range or set that is specified between the brackets." http://msdn.microsoft.com/en-us/library/ms179884.aspx

To get a correct match you can write:

where Message like 'Dear [[]User Name]%'

Upvotes: 2

Related Questions