ASP Classic SQL Server LIKE Query not working properly

Wayne Barron
Wayne Barron used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Wayne BarronAuthor, Web Developer
Top Expert 2009

Author

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.
Author, Web Developer
Top Expert 2009
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

Wayne BarronAuthor, Web Developer
Top Expert 2009

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial