Home > database >  SQL Difference Between "a%" and "a%_"
SQL Difference Between "a%" and "a%_"

Time:12-08

I have been searching on good use-cases and differences between the use of LIKE and = where I faced this problem regarding LIKE.

Since LIKE "_r%" means those with 'r' as their second character, doesn't it hold to assume that "r%_" means those with 'r' as their first character, essentially making it functionally same as "a%".

I am asking this because our lecture slides says the otherwise, and I am not sure whether I am wrong or not. I have also ran this SQL test program (https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_like_underscore) to see it firsthand and this also proves my point.

Have a good day.

CodePudding user response:

No, both r%_ and r% don't actually mean the same thing. The first version r%_ will match any string starting with r, followed by zero or more of any character, followed by any single character. This pattern will match ra and ran, but it will not match single r. The pattern r% on the other hand will match r, since % allows for zero characters following the leading r.

CodePudding user response:

The difference is that a% will match anything that starts with a, including a by itself (% matches zero, one, or multiple characters), while a%_ will match anything that starts with a, but must be followed by at least one other character (_ matches exactly one character).

  • Related