How do I provide a string with a list of values to an "IN" statement

Posted by Degan on Stack Overflow See other posts from Stack Overflow or by Degan
Published on 2010-05-23T19:34:56Z Indexed on 2010/05/23 19:41 UTC
Read the original article Hit count: 136

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about sql