Back to basics - SQL Wild Cards

It's funny how the simplest of things could actually fool you. If you want to search for a particular String, let's say "Lamia", but you don't know if that name actually has some trailing numbers or something. You just know that it starts with "Lam". How do you search for this in SQL? I had this as a question in one interview and because maybe I had very little exposure to this keyword, I ended up using the regexp keyword in MySQL when the answer was just a simple WHERE username LIKE "me".

While working on a feature for my current project, I was searching for a username. So as an example, I use the username "Lamia" but don't know that it really is Lamia. I just know that it starts with "Lam". I tried the following

 

SELECT * FROM users_tbl WHERE username LIKE "Lam"

 
It didn't work. I approached my team lead (quite an intelligent person) and he told me to put a "%" in the end of the string.

SELECT * FROM users_tbl WHERE username LIKE "Lam%"

Now, I swear I knew what the "%" symbol mean. But I really couldn't remember so I searched w3schools. Okay, so the percent symbol is used as some sort of wildcard. I couldn't hate myself more! But again, this proves me wrong that I learn nothing in my current job. Lolz!

Published 09-04-2007 12:04 AM by lamia
Filed under:

Comments

Tuesday, September 04, 2007 4:44 PM by adonhis

# re: Back to basics - SQL Wild Cards

Haven't you remember that query string way back on our college thesis project? :-p

Wednesday, September 05, 2007 10:12 PM by lamia

# re: Back to basics - SQL Wild Cards

Sorry mastah... -_-

Tuesday, September 11, 2007 1:19 AM by modchip

# re: Back to basics - SQL Wild Cards

Haha, funny! "Sometimes it's the basics we forget..." Like in SF, when we already master to do the long strings, we sometimes forget how 'more' effective a simple 2-in-one combo can be. (Plus a little cheap shots) :D