sql server - T sql select int value as casting varchar -
this question has answer here:
- parameterize sql in clause 38 answers
- how pass table value parameters stored procedure .net code 4 answers
i have stored procedured called getreport
customers.branches_id int type
however @branches parameter varchar
@branches "10,13534,554,776,767"
i want search 10,13534,554,776,767 in branches_id if cast/convert varchar not working me.
alter procedure [dbo].[getreport] @fromdate datetime, @todate datetime, @branches varchar (500) = null begin set nocount on; select * customers (customers.createddate between @fromdate , @todate) , (@branches null or convert(varchar(500), customers.branches_id) in(@branches )) -- part not working me end
how can solve problem how can search multiple comma varcvhar inside of int column ?
thanks.
there @ least 4 methods this. here one:
select * customers customers.createddate between @fromdate , @todate , (@branches null or branches_id in( select y.i.value('(./text())[1]', 'nvarchar(4000)') ( select x = convert(xml, '<i>' + replace(@branches, ',', '</i><i>') + '</i>').query('.') ) cross apply x.nodes('i') y ( )))
look other methods here http://blogs.msdn.com/b/amitjet/archive/2009/12/11/sql-server-comma-separated-string-to-table.aspx
but can use table valued types type of work if use sql server 2008 or higher.
Comments
Post a Comment