sql server - Conversion failed when converting the varchar value ',1,2,3' to data type int -


this error results when attempting use comma delimited parameter in in condition

i'm passing varchar parameter stored procedure looks this

,1,2,3 

and want find out if contains 1 (it doesn't contain 1)

what's easiest way in tsql ?

declare @nums varchar(max)=',1,2,3' if 1 in (@nums) -- conversion error begin        select * testtable end 

you need use see if string contains character 1. note match 12 or string character '1' in it.

declare @nums varchar(max)=',1,2,3' if @nums '%1%' begin        select * testtable end 

if need match full number:

create function [dbo].[split_string] (     @itemlist nvarchar(4000),      @delimiter char(1) ) returns @idtable table (item varchar(50))          begin         declare @tempitemlist nvarchar(4000)     set @tempitemlist = @itemlist      declare @i int         declare @item nvarchar(4000)      set @tempitemlist = replace (@tempitemlist, ' ', '')     set @i = charindex(@delimiter, @tempitemlist)      while (len(@tempitemlist) > 0)     begin         if @i = 0             set @item = @tempitemlist         else             set @item = left(@tempitemlist, @i - 1)         insert @idtable(item) values(@item)         if @i = 0             set @tempitemlist = ''         else             set @tempitemlist = right(@tempitemlist, len(@tempitemlist) - @i)         set @i = charindex(@delimiter, @tempitemlist)     end      return end   declare @nums varchar(max) = ',1,2,3'  declare @numbertable table (item int)  insert @numbertable   select try_cast(item int)   dbo.split_string(@nums, ',')  if (select 1 @numbertable item = 1) = 1   begin     select * testtable   end 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -