Searching for the Wildcard Percentage Symbol in SQL Server – when it’s being itself

When searching data stored in a table in an SQL Server database you create queries such as…

Select * from tbl_document were document_name like ‘File %’

This will return all documents with a filename starting with the word File and a space followed by anything. That’s what the % represents – anything, anything at all.

Anything is quite a big concept for a line and two circles when you think out it

But what if you want to search for the % character itself – when it’s having a day off from being the all encompassing wildcard of wonder and it just being a plain, simple percentage?

I had to find all documents in the database that had been uploaded with %20 in the filename – I’ve done this before but for the life of me I couldn’t remember how – so I went to Google but alas the search gods were not smiling on me – so I asked Twitter.

The knights in shining armour that are Ian Field (@IanField90) and Peter Curd (@PCurd) rushed to my rescue with the solution…

where document_name like ‘%[%]%’

This will match anything-percentage-anything.

Once again, knowing the right people on twitter helps. Ok so I’ve met both of these charming gents in real life, but I also received help and suggestions from others who I haven’t.

Happy Querying!