Converting String List into Int List in SQL -
i have nvarchar(max) in stored procedure contains list of int values, did it not possible pass int list stored procedure, but, getting problem datatype int , want compare list of string. there way around can same?
---myquerry----where status in (@statuslist)
but statuslist contains string values not int, how convert them int?
update:
use [database] go set ansi_nulls on go set quoted_identifier on go alter procedure [dbo].[sp] ( @fromdate datetime = 0, @todate datetime = 0, @id int=0, @list nvarchar(max) //this list has string ids// )
as set fmtonly off; declare @sql nvarchar(max), @paramlist nvarchar(max)
set @sql = 'select ------ , code in(@xlist) , -------------' select @paramlist = '@xfromdate datetime,@xtodate datetime,@xid int,@xlist nvarchar(max)' exec sp_executesql @sql, @paramlist, @xfromdate = @fromdate ,@xtodate=@todate,@xid=@id,@xlist=@list print @sql
so when implement function splits not able specify charcter or delimiter not accepting (@list,',').
or (','+@list+',').
it possible send int list stored procedure using xml parameters. way don't have tackle problem anymore , better , more clean solution.
have @ question: passing array of parameters stored procedure
or check code project: http://www.codeproject.com/articles/20847/passing-arrays-in-sql-parameters-using-xml-data-ty
however if insist on doing way use function:
create function [dbo].[fnstringlist2table] ( @list varchar(max) ) returns @parsedlist table ( item int ) begin declare @item varchar(800), @pos int set @list = ltrim(rtrim(@list))+ ',' set @pos = charindex(',', @list, 1) while @pos > 0 begin set @item = ltrim(rtrim(left(@list, @pos - 1))) if @item <> '' begin insert @parsedlist (item) values (cast(@item int)) end set @list = right(@list, len(@list) - @pos) set @pos = charindex(',', @list, 1) end return end
call this:
select * table status in (select * fnstringlist2table(@statuslist))
Comments
Post a Comment