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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 delimited string does not work
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

magmo
Aged Yak Warrior

524 Posts

Posted - 02/01/2014 :  07:36:22  Show Profile  Reply with Quote
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

524 Posts

Posted - 02/01/2014 :  11:11:01  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 02/02/2014 :  01:52:27  Show Profile  Reply with Quote
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
  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.05 seconds. Powered By: Snitz Forums 2000