SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Where Clause IN Statement


Where Clause IN Statement

Author
Message
patelmohamad
patelmohamad
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 765
Hi Guys,
i am stuck in listing some of the data,
My issues is i am providing a list of the branchs or the Value from User interface
'''PLEXITECH'' , ''MOMBASA'' , ''WIPRO'''

the following code i have writen in sql i want to implement "Case 2"
Please suggest on it
DECLARE @Parameter varchar(max)
SET @Parameter = '''PLEXITECH'' , ''MOMBASA'' , ''WIPRO''';
-- Case 1
DECLARE @Query NVARCHAR(max)
SELECT @Query = N'SELECT * FROM M_BRANCH WHERE BRANCHNAME IN (' + @Parameter + ')'
EXEC sp_executesql @Query

-- Case 2 I Want to implement the following query how should i execute it, any suggestion regarding same
SELECT * FROM M_BRANCH WHERE BRANCHNAME IN ( @Parameter )

Patel Mohamad
Subbu S
Subbu S
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 138
Dear Frnd,

You can achieve ur requirement through the following way,


Declare @s varchar(100)
Set @s = 'Subbu,Subbiah'
select * from employee WHERE namess in ( SELECT value COLLATE DATABASE_DEFAULT FROM SplitList(@s , ',') )

First Declare the variable with the PARAMETERS u need
Then give the following line in WHERE CLAUSE

SELECT value COLLATE DATABASE_DEFAULT FROM SplitList(@s , ',')


SplitList is a function. I had given the funtion






Create FUNCTION [dbo].[SplitList]
(
@List nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(

Id int identity(1,1),
Value nvarchar(100)
)
AS
BEGIN

While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End

Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))

Return
END






Please let me know, it helped u r nt
patelmohamad
patelmohamad
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 765
Thanks subbusa2050

it works and implemented in my project.

Regards

Patel Mohamad
Subbu S
Subbu S
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 138
Hi Frnd,

Ya ok fine :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search