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

how to pass 1,2,3,---- as input for ID

and

how to extract them into invidual to check the different combinations existed in the column of mytable
Posted

Try this.
First create a function like bellow...
SQL
 CREATE FUNCTION [dbo].[CheckCombination]
 (
  @Ids VARCHAR(100)
  ) 
 RETURNS VARCHAR(20)
AS 
BEGIN

DECLARE @Msg VARCHAR(20);

IF EXISTS ( SELECT 1 FROM (
 SELECT ID,STUFF(( SELECT ',' + CAST(ApplicantId AS VARCHAR) FROM ApplicantTable 
 WHERE ID = A.Id FOR XML PATH('') ),1,1,'')Ids
 FROM ApplicantTable A GROUP BY ID ) A WHERE Ids = @Ids )

 SET @Msg = 'Exists'

ELSE

 SET @Msg = 'Not Exists'

RETURN @Msg
END


After creation, test the function....

SQL
SELECT dbo.CheckCombination('1,2,3')



Thank you
 
Share this answer
 
v2
Comments
sk. maqdoom ali 8-Nov-12 5:27am    
thank u , but here one problem is
i already inserted
ID Applicant_Member_id
18 39,39,39,51,39
102 41,51
155 39,217

now again i am giving 51,41 for any ID then this must not be display as 'Notexists'
becoz the 51,41 is already existed.
here we changed the order only not numbers.
how can we come across this problem
sk. maqdoom ali 8-Nov-12 5:50am    
sir , how can we check if there is any existed value in combination for this
sk. maqdoom ali 8-Nov-12 5:59am    
i used this function for checking the combination of existed values but its is not checking if the combination is reversed etc

CREATE FUNCTION [dbo].[Check_MemberIDCombination]
(
@Ids VARCHAR(1000)
)
RETURNS VARCHAR(20)
AS
BEGIN

DECLARE @Msg VARCHAR(20);

IF EXISTS ( SELECT 1 FROM (
SELECT ID,STUFF(( SELECT ',' + CAST(Applicant_Member_Id AS VARCHAR) FROM Savings_Account_Applicant
WHERE ID = A.Id FOR XML PATH('') ),1,1,'')Ids
FROM Savings_Account_Applicant A GROUP BY ID ) A WHERE Ids = @Ids )


SET @Msg = 'Already Exists'

ELSE

SET @Msg = 'Available'

RETURN @Msg
END

Not as simple as you might think:
SQL
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '2,3,177,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab (id int not null)
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO #tempTab (id) VALUES (@VALUE)
END
SELECT * FROM myTable WHERE id IN (SELECT id FROM #tempTab)
DROP TABLE #tempTab
 
Share this answer
 
Comments
sk. maqdoom ali 8-Nov-12 3:24am    
yes, u r correct
i have a table with ID , Applicant_member_Id columns
when we are inserting one or more Applicant_member_Id's for ex:39,41,116 as input then i have to check entire this combination 39,41,116 is already existed for any particular ID in the table. if the combination is existed then we display "Already Existed' else 'available'
sk. maqdoom ali 8-Nov-12 3:34am    
tell me sir, how can i check this combination of values based on ID in a table??
OriginalGriff 8-Nov-12 3:59am    
So, is Applicant_member_Id a string containing comma separated values? Or a number? I think you need to give me a bit more info here! (Remember I can't see your screen, or read your HDD)
sk. maqdoom ali 8-Nov-12 4:06am    
yes sir , Applicant_member_Id is a string containing comma separated value
ex:input=41,42,43,---
i have to check any ID in my table having this combination 41,42,43
if this type of combination is existed then dislay msg as 'Already Existed'
else we can insert
sk. maqdoom ali 8-Nov-12 4:08am    
in my table Applicant_member_Id is a column having only a single value for particular ID
ex:101 41
102 42
103 41
- -
- -
Create 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


The above function will return a temporary table.

Sample code:

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


Output:
Data
1
2
3
4
 
Share this answer
 
v2

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