Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Using Like % and String Variable in a sql statemen

Author  Topic 

kierandes
Starting Member

6 Posts

Posted - 2010-02-19 : 20:20:52
Hey all,
I'm a bit of a noob when it comes to sql so bare with me.

I am using jsp sessions to retrieve a request. then I am putting this posted request data in a string called SongTitle. My next question is how do I use the % wildcard with the variable? I've tried it like I have it below but I get errors with it. any help would be greatly appreciated.
like  % '" +  SongTitle + "'%

X002548
Not Just a Number

15586 Posts

Posted - 2010-02-19 : 20:39:59
'%' + SongTitle + '%'

BUT this will cause a scan

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

kierandes
Starting Member

6 Posts

Posted - 2010-02-19 : 20:45:24
Thanks Brett. just tried that there and no luck. got more server errors.
The String only seems to take the data in when i have it like '" + SongTitle + '".
Go to Top of Page

kierandes
Starting Member

6 Posts

Posted - 2010-02-19 : 20:49:06
Ahh Sorted it, heres what it looks like now.
like  '%"+  SongTitle +"%'
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-02-19 : 21:03:41
ummmm..I'm guessing it'snot T-SQL


Unless the QUOTE option is set different than out of the box...

Well I'm glad it's fixed




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-20 : 01:59:57
Brett: I reckon it is dynamic SQL in the application language (JSP)

kierandes: You need to protect the data the user entered from SQL Injection (Google if you have not heard of it), otherwise people can put stuff in your "songtitle" field that will do bad stuff (grab your passwords, mess with the data, delete the whole database, ... etc )
Go to Top of Page

kierandes
Starting Member

6 Posts

Posted - 2010-02-20 : 05:00:42
Thanks Kristen,
I'll take on board :)
Go to Top of Page

kierandes
Starting Member

6 Posts

Posted - 2010-02-20 : 07:40:49
grr thought it was sorted but the wildcard dosent seem to be working. i need to type in the exact name or part of the name to get results. ie
Time is Running Out vs time is running out. the first gets results unlike the other. any ideas?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-20 : 08:00:16
So your database column is case sensitive?

Use

like '%"+ SongTitle +"%' COLLATE SQL_Latin1_General_CP1_CI_AS

but you may need to choose a better collation (using the same one as the [SongTitle] column is defined with but changing the suffix to "_CI_AS" would probably be best)
Go to Top of Page

kierandes
Starting Member

6 Posts

Posted - 2010-02-20 : 08:10:45
Hmm it didnt seem to like that query either. I'm using Java DB and theres no options for collation but ya it seems to be case sensitive.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-20 : 08:18:51
" I'm using Java DB "

This is specifically a Microsoft SQL Server forum, so you may not find people here who know the answer.

Do you have a function to force a string to upper case? Is so use it on both sides of the LIKE

UPPER(MyColumn) like UPPER('%"+ SongTitle +"%')
Go to Top of Page
   

- Advertisement -