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)
 sring formation with delimiter from table

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2014-02-26 : 06:12:09
Hi,
I am having table t1 having column c1 with values v1, v2, v3

I need to form a dynamic query which needs to consider the t1 table's c1 values in where condition.

dynamic sample query:

select name from Table
Where value in (v1,v2,v3).

Thanks
Subha

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-02-26 : 08:15:13
Don't quite know what you meant by "dynamic". If you mean that they will come in as parameters, there are a few different ways you could handle it.

One would be to put those values into a table and join on that. For example:
DECLARE @Table2 TABLE(VALUE VARCHAR(32));
INSERT INTO @Table2 VALUES ('v1'),('v2'),('v3');

SELECT t1.name FROM Table1 t1
INNER JOIN @Table2 t2 ON t1.VALUE = t2.VALUE
If you are getting the values as a string delimited parameter, you can first use a string splitter to split the string into the 3 tokens and use the above approach. Or, you could do something like this:
DECLARE @parameterString VARCHAR(32) = 'v1,v2,v3';
SELECT name FROM Table1
WHERE ','+@parameterString+',' LIKE '%,'+VALUE+',%';
This assumes VALUE is a string column.
Go to Top of Page
   

- Advertisement -