Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i need to compare the values which i will pass from my end then i want to compare the values with an array

using this SELECT SUBSTRING( (SELECT ',' + CONVERT(varchar, Applicant_Member_Id) FROM Savings_Account_Applicant SAA,Savings_Account SA WHERE SA.ID=SAA.ID AND SA.Account_Operation_Type = 2 FOR XML PATH('')),2,100000) AS CSV i am getting array of values 41,46,46,41,41,41,41,39,38,202,202,205,205,150,150,150,150,41,41,187 but in my procedure i will give only one value as input ie,id=41 for this i want to check the id value is existed in that array or not
Posted
Updated 7-Nov-12 1:36am
v2
Comments
Om Prakash Pant 7-Nov-12 7:29am    
not clear, can you please elaborate?
sk. maqdoom ali 7-Nov-12 7:33am    
using this SELECT SUBSTRING(
(SELECT ',' + CONVERT(varchar, Applicant_Member_Id) FROM Savings_Account_Applicant SAA,Savings_Account SA WHERE SA.ID=SAA.ID AND SA.Account_Operation_Type = 2
FOR XML PATH('')),2,100000) AS CSV
i am getting array of values 41,46,46,41,41,41,41,39,38,202,202,205,205,150,150,150,150,41,41,187

but in my procedure i will give only one value as input ie,id=41 for this i want to check the id value is existed in that array or not

You can convert your array to table use below function

SQL
CREATE FUNCTION [dbo].[String_Tokenizer]
(
	@RowData nvarchar(max),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1
 
	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
 
		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))
 
	Return
END


Sample code:

SQL
select * from [dbo].[String_Tokenizer]('1,2,3,4',',')


Output: 
Data
1
2
3
4


for comparing

SQL
select * from [dbo].[String_Tokenizer]('1,2,3,4',',') as Tmp where data=2 -- your id
 
Share this answer
 
Comments
Shanalal Kasim 8-Nov-12 6:58am    
If it helped you to solve your problem , mark it as answer
We can achieve this by using XQuery. For this we need to create a function like following..
SQL
CREATE FUNCTION [dbo].[Splitter](
    @CSVData nvarchar(max),
    @SplitOn nvarchar(5)
)
RETURNS @SplitterValues table 
(
	Data INT
)
AS 
BEGIN
  DECLARE @Xparam XML;

  SELECT @Xparam = CAST('<i>' + REPLACE(@CSVData,@SplitOn,'</i><i>') 
  +  '</i>' AS XML) 

  INSERT INTO @SplitterValues SELECT x.i.value('.','INT')  FROM 
  @Xparam.nodes('//i') x(i)

  RETURN

END


Now we have to use this in query..
SQL
 DECLARE @Id INT;
 DECLARE @CsvData VARCHAR(MAX)

 SET @ID = 41;
 SET @CsvData = '41,46,46,41,41,41,41,39,38,202,202,205,205,150,150,150,150,41,41,187'

 SELECT 1 WHERE @Id IN
(SELECT Data FROM [dbo].[Splitter](@CsvData,','))


I hope this answer helps you..

Thank you..
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900