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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 delimited string does not work

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2014-02-01 : 07:36:22
Hi


I try to use a delimeted string in a Query like this....



DECLARE @Books nVarChar(500)
SET @Books = 'Book1,Book2'

....

WHERE (dbo.Books.BookName IN (@Books)) AND (dbo.Books.Language = 'se') AND (dbo.Books_1.Language = 'gb')



Its not working, it only works with one book name... what could be wrong?

magmo
Aged Yak Warrior

558 Posts

Posted - 2014-02-01 : 11:11:01
For some reason WHERE ';'+@Books+';' LIKE '%;'+CAST(dbo.Books.BookID AS VARCHAR(32))+';%' works, I gonna have to use that instead..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-02 : 01:52:27
quote:
Originally posted by magmo

For some reason WHERE ';'+@Books+';' LIKE '%;'+CAST(dbo.Books.BookID AS VARCHAR(32))+';%' works, I gonna have to use that instead..


it does pattern matching and tries to match ; + BookID + ; within the passed parameter list which is exactly what you want
Only thing is for large data performance might be an issue
so you may need to use a parsing udf and parse values out from delimited parameter list and add a join to it from your table

http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html

http://visakhm.blogspot.in/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -