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
 New to SQL Server Programming
 Search with comma seperated input

Author  Topic 

SQLNoob81
Starting Member

38 Posts

Posted - 2013-03-18 : 09:47:51
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-18 : 10:07:06
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

52326 Posts

Posted - 2013-03-18 : 12:51:36
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

38 Posts

Posted - 2013-03-19 : 06:13:38
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-19 : 06:41:20
May be try this...

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



--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-19 : 10:03:49
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
   

- Advertisement -