1

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.

Marwan
  • 1,058
  • 1
  • 11
  • 20
  • 1
    There is a question already posted. See this. http://stackoverflow.com/questions/8984380/sql-server-need-to-escape – Jayy Feb 21 '12 at 07:07
  • I'm using SQL Server 2008 R2. And yes, this question is a duplicate of http://stackoverflow.com/questions/8984380/sql-server-need-to-escape, so I will close it. Thank you. – Marwan Feb 21 '12 at 07:44

2 Answers2

2

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]%'
Marwan
  • 1,058
  • 1
  • 11
  • 20
2

try this

 where Message like 'Dear \[User Name\]' escape '\';
Vikram
  • 8,235
  • 33
  • 47