views:

67

answers:

3

I am creating a string that is a list of comma-delimitted values by looping through the selections in a CheckBoxList. I am able to display this value, so I know that it is creating what I expect. I am attempting to pass this list to an IN statment in a SELECT query:

SelectCommand="SELECT ThisDate, DATEPART(dw, ThisDate) AS Expr1 FROM fbCalendar WHERE (ThisDate >= @ThisDate) AND (ThisDate <= @ThisDate2) AND (DATEPART(dw, ThisDate) IN (@TheseDays))"

<asp:ControlParameter ControlID="Label1" Name="TheseDays" PropertyName="Text" Type="String" />  

This works fine as long as there is only a single item selected, but selecting a second item fails with the message: Conversion failed when converting the nvarchar value '4,5' to data type int.

However, I do not understand when this would be converted to an INT. I have tried many different formatting attempts (such as encapsulating the string in parenthesis (e.g. "(4,5)" ) for the SELECT query, but I have yet to find the right one to make this work. It seems like formatting is the problem, but perhaps I am missing something else.

+1  A: 

You should see this post on SO for the answer.

Zachary
The top answer there suggests: select @Sql = 'select [Name] from Department where DepartmentId in (' + @DepartmentIds + ')' I then try:SelectCommand="SELECT ThisDate, DATEPART(dw, ThisDate) AS Expr1 FROM fbCalendar WHERE (ThisDate >= @ThisDate) AND (ThisDate <= @ThisDate2) AND (DATEPART(dw, ThisDate) IN (' + @TheseDays + '))"Which results:Conversion failed when converting the varchar value ' + @TheseDays + ' to data type int.I have tried some variations on single and double quotes, but perhaps something else is going wrong, most do not build.
Degan
+1  A: 

It is because, you are trying to pass a parameter with the way cannot be done with IN statement.

Take a look that post which explains some other ways that you may use. SQL - Stored Procedure with Select Statement using IN (@Variable_CommaDelimitedListOfIDS)

Emrah GOZCU
Similar link at nearly the same time, I guess my searching was poor. I still have not seen anything that works in this case. I will keep looking through it to see if I can find a useful method. I did not expect this to be so difficult.
Degan
@Degan: Your case a bit different. You are using some MS helper custom controls. I am not sure that this user control supports table parameters, if it does, go for table parameter, but if not, I suppose you should create an sp and use the methods that is explained the link i have sent in my answer.
Emrah GOZCU
A: 

Degan.

I created a table to store the values to pass to the query. It just doesn't seem right.

You don't have to create an actual table. You can use a Table Valued Function and use that in your query e.g. as below.

(DATEPART(dw, ThisDate) IN (SELECT [item] FROM dbo.fnSplit(@TheseDays, ',')))

This uses the TVF from http://www.eggheadcafe.com/community/aspnet/13/10021854/fnsplit.aspx).

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000) -- List of delimited items
  , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
 SELECT
  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
  @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

 IF LEN(@sItem) > 0
  INSERT INTO @List SELECT @sItem
 END

IF LEN(@sInputList) > 0
 INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
Martin Smith