ASP Classic SQL Server LIKE Query not working properly

Hello All;

The record exist in the database, in both columns, so, it SHOULD state that the record exist.
However, it is not, it is stating that the record does not exist.
Even though it is there.

We are looking at BOTH columns, as the record will exist, ONLY if both columns have the correct information.

The PageList Column, this will have different entries in it like so.

Movies_(Romance) / Movies_(Horror) / Movies_(Comedy) / Movies_(Disney) / Movies_(Drama)

So, that is why I am using the LIKE, so that I can check and see if the "Movies_(Romance)" exist in the column. If not, then add it, else, NOT.

Can someone please shine some light on this?
As I am at my wits end, I do not know what in the world is going wrong.
I got the same code to work in another part of the page, however, I cannot get this one to function.


One last thing.
I ran the same Query inside of SSMS, and it ran perfectly, and like expected, it is just NOT working in the script below.

Carrzkiss

Code:
getTitle = "Movies_(Romance)"
SongName = "Let Me Know"
<%
' Both getTheSong and getTitle are coming from form fields.'
getTheSong = replace(SongName," ","_")
strTerm = "%" & getTitle & "%"

Set sqlUsage = Server.CreateObject("ADODB.Command")
sqlUsage.ActiveConnection=Conn
sqlUsage.Prepared = true
sqlUsage.commandtext="SELECT SongTitle, PageList FROM Songs WHERE SongTitle=? and PageList like ?"
sqlUsage.Parameters.Append sqlUsage.CreateParameter("@SongTitle", adVarChar, adParamInput, 250, getTheSong)
sqlUsage.Parameters.Append sqlUsage.CreateParameter("@PageList", adVarChar, adParamInput, 250, strTerm) 
set rschSong = sqlUsage.execute
if rschSong.eof then
response.write "Does not exist"
else
response.write "Already exist!"
end if
%>

Open in new window

LVL 31
Wayne BarronAuthor, Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne BarronAuthor, Web DeveloperAuthor Commented:
To add in.
The other code, does not work either, so, once I try to look at 2 columns, with the LIKE, it breaks.

I have also tried: Brackets around the underscore, to see if that was the issue, and nothing... [_]
I have tried the !_ escape '!'
And still nothing.

However, using the escape in SSMS 2012 works.
Using the [_] in SSMS 2012, DOES NOT work.

Now, I will be using this on the production server, and they are running SQL Server 2005 currently.
So, whatever we can get to work here, must be cross-platform, working on both 2005 and 2012.
Wayne BarronAuthor, Web DeveloperAuthor Commented:
Got it!!!
Changed the LIKE for charindex
charindex(?, PageList)>0
And that fixed the issue, now both work awesomely!
Just will have to find out if it works with SQL Server 2005 next.

<%
getForm = ProtectSQL(Request.Form("Submit"))
' Both getTheSong and getTitle are coming from form fields.'
getTitle = protectSQL(Request.Form("Title"))
SID  = protectSQL(Request.Form("songID"))
SongName  = protectSQL(Request.Form("SongName"))

getTheSong = replace(SongName," ","_")
strTerm = "%" & reverseHelp(getTitle) & "%"
response.write getTheSong&"<br />"&strTerm&"<br />"

Set sqlUsage = Server.CreateObject("ADODB.Command")
sqlUsage.ActiveConnection=EV_WikiConn
sqlUsage.Prepared = true
sqlUsage.commandtext="SELECT SongTitle, PageList FROM Songs WHERE SongTitle=? and charindex(?, PageList)>0"
sqlUsage.Parameters.Append sqlUsage.CreateParameter("@SongTitle", adVarChar, adParamInput, 250, getTheSong)
sqlUsage.Parameters.Append sqlUsage.CreateParameter("@PageList", adVarChar, adParamInput, 250, reverseHelp(getTitle)) 
set rschSong = sqlUsage.execute
if rschSong.eof then
response.write "Does not exist"
else
response.write "Already exist!"
end if
%>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne BarronAuthor, Web DeveloperAuthor Commented:
This did it.
Love that CharIndex... Worked a charm on this one.
It works across all platforms of SQL Server 2005 and up.

Carrzkiss
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.