Question

I'm attempting to query our MSSQL database but I'm getting no data when there clearly is data there.

First I query

SELECT id, instruction_link FROM work_instructions WHERE instruction_link LIKE "%\\\\cots-sbs%";

Which returns 100+ lines. http://tinypic.com/r/ief8td/8 (sorry couldn't post as actual picture, don't have enough rep :(

However if I query

SELECT id, instruction_link FROM work_instructions WHERE instruction_link LIKE "%\\\\cots-sbs\\%";

http://tinypic.com/r/33ksw3q/8

I get no results with the 2nd query. I have no idea what I'm doing wrong here. Seems pretty simple but I can't make any sense of it..

Thanks in advance.

Was it helpful?

Solution

As documented under LIKE:

Note

Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.

\\% is parsed as a string containing a literal backslash followed by a percentage character, which is then interpreted as a pattern containing only a literal percentage sign.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top