July 18, 2012 at 5:11 am
Hi I need to take the result by passing more ID, below is the sample query, I am getting error converting vachar to INT ERROR. Because data type for metric id is INT.
Is there any way to get the result
declare @v3 varchar(21) = '66,67,68'
select * from MasMetrics where MetricsId in ( cast(@v3 as int)) -- error
July 18, 2012 at 5:20 am
Edited: Incorrect response removed.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 18, 2012 at 5:25 am
Hi,
If i use ur Query I am getting the result like 666768. But I need to pass the value as 66,67,68
July 18, 2012 at 5:31 am
vijayarani87.s (7/18/2012)
Hi I need to take the result by passing more ID, below is the sample query, I am getting error converting vachar to INT ERROR. Because data type for metric id is INT.Is there any way to get the result
declare @v3 varchar(21) = '66,67,68'
select * from MasMetrics where MetricsId in ( cast(@v3 as int)) -- error
you need to use a split function here
@v3 is a string which happens to contain commas; instead you need to split the string into a table so your query can use the IN function correctly.
(search for DelimitedSplit8K on this site)
declare @v3 varchar(21) = '66,67,68'
SELECT *
from MasMetrics
where MetricsId in ( SELECT Item from dbo.DelimitedSplit8K (@v3) )
Lowell
July 18, 2012 at 5:36 am
try the following solution
http://www.codeproject.com/Articles/6083/Passing-comma-delimited-parameter-to-stored-proced
July 18, 2012 at 5:39 am
yes this is the correct solution, Do u have any function for that
July 18, 2012 at 5:41 am
Thank u Swarna 🙂
July 18, 2012 at 5:43 am
the function attached to this article is recommended by myself any my peers:
http://www.sqlservercentral.com/articles/Tally+Table/72993/
Lowell
July 18, 2012 at 7:27 am
Lowell (7/18/2012)
the function attached to this article is recommended by myself any my peers:
+1
Definitely. The other method uses a While loop which might give performance issues.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 18, 2012 at 7:38 am
Kingston Dhasian (7/18/2012)
Lowell (7/18/2012)
the function attached to this article is recommended by myself any my peers:+1
Definitely. The other method uses a While loop which might give performance issues.
+1 again. 😀
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply