convert string into int SQL Server -


this scenario:

my app have following:

  1. a listbox (the checkbox property enabled) display list of something.
  2. the user select listbox (multiselect) using checkbox.
  3. i loop checked items , store id's array. store id's separating id comma (1,2,3,4) , use length -1 delete last comma.

how can convert string 1,2,3,4 integer type of data if stored procedure this?

select * tblsomething id in (1,2,3,4) 

you can use following sql function.

set ansi_nulls on go  set quoted_identifier on go  create function [dbo].[commaseparatedtostring] (    @pscsstring varchar(8000) )  returns @ottemp table(sid varchar(20)) begin declare @stemp varchar(50) while len(@pscsstring) > 0 begin set @stemp = left(@pscsstring, isnull(nullif(charindex(',', @pscsstring) - 1, -1),                 len(@pscsstring))) set @pscsstring = substring(@pscsstring,isnull(nullif(charindex(',', @pscsstring), 0),                            len(@pscsstring)) + 1, len(@pscsstring)) insert @ottemp values (@stemp) end return end 

and call in stored procedure

select * tblsomething  id in (select * commaseparatedtostring('1,2,3,4')) 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -