Wildcard for single characters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Wildcard for single characters

When you use the underscore for a comparison of string in the where statement, you only use it for a single character. I was wondering if you have this statement: WHERE name LIKE 'a_b' Will it gather data like 'ab', or do you have to have a single character between a and b for it to be true? If that's true, why?

11th Mar 2024, 3:01 AM
Colton Minard
Colton Minard - avatar
2 Answers
+ 2
In a SQL `LIKE` statement with an underscore (`_`) as a wildcard, it represents a single character. So, in your example `WHERE name LIKE 'a_b'`, it would match strings like 'aab', 'acb', but not 'ab' because there must be a single character between 'a' and 'b'. The underscore is a placeholder for any single character, ensuring there is exactly one character in that position for the match to occur. If you want to allow zero or more characters between 'a' and 'b', you can use the percent sign (`%`) as a wildcard: `WHERE name LIKE 'a%b'`. This would match strings like 'ab', 'axb', 'a123b', etc.
11th Mar 2024, 8:19 AM
Knight
Knight - avatar
+ 6
Colton Minard in general it should pick up at 'a' and then a single character and then 'b'. It should not pick up say words like able or stable or label. The wildcard says there is a letter or number in this case for product identification as 'a1b' or 'azb' given that this is pattern matching ...
11th Mar 2024, 3:55 AM
BroFar
BroFar - avatar