convert string into int SQL Server -
this scenario:
my app have following:
- a listbox (the checkbox property enabled) display list of something.
- the user select listbox (multiselect) using checkbox.
- 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
Post a Comment