SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 split string and compare
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/19/2013 :  07:39:57  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
I have 2 parameters in my Stored Procdure. I want to find the values not in 2nd parameter

str_emp_chennai = 'Raj,Kumar,Selvam'

str_emp_delhi = 'Kannan,Raj,Gopal'

I need result to be

str_new_emp = 'Kumar,Selvam'

The employee and the name as for represntaion. my scneario have other values. kinldy help me with logic for spliting and comparing string based on ","


THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 04/19/2013 :  08:05:10  Show Profile  Reply with Quote
Create one UDF to split CSV and then run the following query
-- UDF: CustomSplit
CREATE FUNCTION [dbo].[CustomSplit] (@sep char(1), @s varchar(4000))
RETURNS table
AS
RETURN (
WITH Pieces(n, start, stop) AS (
SELECT 1, 1, CHARINDEX(@sep, @s)
UNION ALL
SELECT n + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT n,
SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS Val
FROM Pieces
)
GO

-- Query to get required output
DECLARE @str_emp_chennai VARCHAR(1000)= 'Raj,Kumar,Selvam'

DECLARE @str_emp_delhi VARCHAR(1000) = 'Kannan,Raj,Gopal'

SELECT STUFF((SELECT ','+ Val 
				FROM (SELECT Val FROM CustomSplit(',', @str_emp_chennai)
						EXCEPT
						SELECT Val FROM CustomSplit(',', @str_emp_delhi)
					 ) t
				FOR XML PATH ('')
			), 1, 1, '')


--
Chandu
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/19/2013 :  08:12:32  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
can you give me in stored procedure

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 04/19/2013 :  08:19:12  Show Profile  Reply with Quote
whats the problem with UDF (user-Defines Function) for Splitting?



--
Chandu
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/19/2013 :  08:36:04  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
Since we dont have any function in this project till now , i would like to have in SP if possible

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 04/19/2013 :  09:09:08  Show Profile  Reply with Quote
GO
CREATE PROCEDURE Split_CompareProc(
@Param1 VARCHAR(2000),
@Param2 VARCHAR(2000),
@Result VARCHAR(2000) OUTPUT
)
AS
BEGIN
	
	SELECT @Result = ISNULL(@Result, '') + ',' + wrd
	 FROM 
		(SELECT p.q.value('.', 'varchar(100)') as wrd
		FROM (SELECT CAST('<Values><Value>' + REPLACE( @Param1 , ',', '</Value><Value>')
			+ '</Value></Values>' AS xml) AS m)t
			CROSS APPLY m.nodes('/Values/Value')p(q)
		EXCEPT
		SELECT p.q.value('.', 'varchar(100)') as wrd
		FROM (SELECT CAST('<Values><Value>' + REPLACE( @Param2 , ',', '</Value><Value>')
			+ '</Value></Values>' AS xml) AS m)t
			CROSS APPLY m.nodes('/Values/Value')p(q)
		) tmp
	
	SELECT STUFF(@result, 1, 1, '')
END
GO


DECLARE @str_emp_chennai VARCHAR(1000)= 'Raj,Kumar,Selvam', @result VARCHAR(2000)
DECLARE @str_emp_delhi VARCHAR(1000) = 'Kannan,Raj,Gopal'
EXEC Split_CompareProc @str_emp_chennai, @str_emp_delhi, @result OUT
/*Output: 	Kumar,Selvam */		


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000