Thursday, May 11, 2006

[sql] Matching on underscore in SQLServer

Just learned that SQLServer uses underscore (_) as a single character wildcard (As oppose to %, which matches to mulitple characters). Thanks for techsoda for the trick.

For example, find rows that where [column] contains underscore:

SELECT * from [table] where [column] like '%^_%
' ESCAPE '^'

The ESCAPE keyword specifies the carat as the escape character.