SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Search with comma seperated input
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLNoob81
Starting Member

South Africa
38 Posts

Posted - 03/18/2013 :  09:47:51  Show Profile  Reply with Quote
Hi All
I would like to know how to query a field in a table with comma separated input.

e.g

InputString = 'Math, Geography, History'

select * from
Scholars where subjects = InputString

need to know how to match all and another query to match at least one

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 03/18/2013 :  10:07:06  Show Profile  Reply with Quote
If this (InputString = 'Math, Geography, History' )format is fixed and also small set of values, then use

WHERE ', ' + @InputString + ', ' LIKE '%, ' + subjects + ', %'

Note:
fixed format in the sense value1, value2, valu3

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/18/2013 :  12:51:36  Show Profile  Reply with Quote
see

http://visakhm.blogspot.in/2013/01/string-pattern-search-xml-based-method.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SQLNoob81
Starting Member

South Africa
38 Posts

Posted - 03/19/2013 :  06:13:38  Show Profile  Reply with Quote
Maybe I did not explain myself correctly.

The field (Subjects) would Typically hold text as follows:

Joe Soap took the following Subjects at School, Math, although he was not good at it. History, Avg Student but was very good at geography.

I will have a Text box on a form where you can enter Subject that can be searched. eg. "History, Math, Geography"



Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 03/19/2013 :  06:41:20  Show Profile  Reply with Quote
May be try this...

WHERE subjects LIKE '%' + REPLACE(@InputString, ', ', '%')+ '%'



--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/19/2013 :  10:03:49  Show Profile  Reply with Quote
quote:
Originally posted by bandi

May be try this...

WHERE subjects LIKE '%' + REPLACE(@InputString, ', ', '%')+ '%'



--
Chandu


Hmm..what if the order of subjects are different in string

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000