Question:
SQL LIKE function, am i using it correctly?
Serena
2010-11-09 05:11:33 UTC
Good day all! ^_^

I'm having problem with the simplest sql ever, and i cant point where the mistake is. hoping for extra pairs of eyes to help me out here.

So i have an EventS table with a lot of records, its EventS.nameOfEvent containing the word "Event", so i want to filter out these records and display them out.

my SQL:
SELECT EventS.nameOfEvent
FROM EventS
WHERE EventS.nameOfEvent LIKE '%Event%'


but nothing is returned! gosh please help me out.
Four answers:
TheMadProfessor
2010-11-09 09:23:54 UTC
Just to make sure that case considerations aren't the problem, try this (btw, when you only have one table involved, no reason to qualify the column names):



SELECT nameOfEvent FROM EventS

WHERE lower(nameOfEvent) LIKE '%event%'



Also, as AJ points out, Access (stupidly, imo) uses its own set of wildcard indicators:



? (instead of _) - any single character

* (instead of %) - any number of characters

# - any single digit
Yan
2010-11-09 13:33:19 UTC
SELECT nameOfEvent

FROM EventS

WHERE nameOfEvent LIKE '%Event%'



right there are 2 possiblitys as to why the query above may not work



There are no rows with a nameOfEvent containing the word event

OR

You have a case sensitive colation on the database and have the word event and not Event



Good luck
just "JR"
2010-11-09 14:20:43 UTC
I can't see any obvious error...

However, if your DB is mysql, you could try the "recommended syntax":

$sql = "select * from `EventS` where `nameOfEvent` like '%Event%' ";

$res = mysql_query($sql) or die(mysql_error());

Notice the use of backward single quotes.

The "or die" will at least tell you something.

And as said above, beware of letter's cases: mysql can is case sensitive as far as field names are concerned!
AJ
2010-11-09 15:55:08 UTC
Access has different wildcards than what you are using.



LIKE "*Event*"



if this is in a string then you have to do double double quotes. LIKE ""*Event*""


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...