0

As the title says im trying to do a select statement in my database to find data that begins and ends with square brackets i.e. [Unchecked]

I've tried the following select query with like but it doesn't seem to be correct...

SELECT * FROM [DB_Table] WHERE [DB_Column] LIKE '[%'

This doesn't seem to get any of the data that starts with [. although if i change the [ to a letter such as A it gets all the data beginning with A.

is it possible to do Like '[%' (beginning with) and '%]' (and ending with)?

This link How can I escape square brackets in a LIKE clause? is not a duplicate of mine it is completely different.

pirho
  • 11,565
  • 12
  • 43
  • 70
h.d06
  • 95
  • 1
  • 8

2 Answers2

1

Change your query to

SELECT * FROM [DB_Table] WHERE [DB_Column] Like '[[]%'

You need to wrap the '[' in square brackets to escape it and treat it as text to be used for searching. Note that this is for SQL Server.

Also, if you wanted to search for values starting with '[' and ending with ']', you could do this:

SELECT * FROM [DB_Table] WHERE [DB_Column] Like '[[]%]'
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
  • the second query works great thanks a lot! – h.d06 Jun 03 '14 at 09:48
  • Good to hear that. However, do take note that, as others have pointed out in the comments, research doesn't just mean trying a query. It means referring to the documentation and other sources to see if the problem you are facing is common and has a well-documented solution. – shree.pat18 Jun 03 '14 at 09:51
0

Try this...

SELECT * FROM [DB_Table] 
WHERE [DB_Column] Like '\[%' ESCAPE '\'

OR

SELECT * FROM [DB_Table] 
WHERE [DB_Column] Like '%\[Unchecked\]%' ESCAPE '\'
M.Ali
  • 67,945
  • 13
  • 101
  • 127
  • does this only search for ones beginning with [ ? it does work well just wondering if i add ..ESCAPE ']%\' to the end quote that would be more efficient. – h.d06 Jun 03 '14 at 09:45
  • I dont know exactly what kind of data you have in your table. Whatever suits you pick that method :) – M.Ali Jun 03 '14 at 09:48