troubleshooting Question

SQL String Comparison

Avatar of PAGANED
PAGANEDFlag for United States of America asked on
Microsoft SQL Server
17 Comments1 Solution558 ViewsLast Modified:
We have a table that has problem control codes. These codes are assigned to different teams such as ACCT, MULT, BUYR, DATA, EXPD, and WHSE. Some of these problem codes are assigned more than once. Examples:

ProblemControlID = 3
ProblemGroupString = ACCT, EXPD

ProblemControlID = 9
ProblemGroupString = DATA,ACCT,EXPD

If we cycle the cursor for all the records and concatenate, we would end up with a ProblemGroupStringCombined = ACCT,EXPD,DATA,ACCT,EXPD

We want this result:
ProblemGroupStringCombined  = ACCT, EXPD, DATA

In other words, we don't want to repeat any duplicate groups
Here is our cursor code that we've been trying to play with.
DECLARE @ProblemGroupStringCombined nvarchar(500)
SET @ProblemGroupStringCombined =  ''
DECLARE ProblemGroupStringCursor CURSOR FOR
SELECT ProblemGroupString FROM tblProblemControlTemp WHERE ProjectID = @ProjectID 
OPEN ProblemGroupStringCursor FETCH NEXT FROM ProblemGroupStringCursor INTO @ProblemGroupString
	IF @ProblemGroupString is not null
		SET @ProblemGroupStringCombined = case when charindex(isnull(@ProblemGroupString,''), isnull(@ProblemGroupStringCombined,'')) = 0 then isnull(@ProblemGroupStringCombined,'') + isnull(@ProblemGroupStringCombined,'') else isnull(@ProblemGroupStringCombined,'') end
FETCH NEXT FROM ProblemGroupStringCursor INTO @ProblemGroupString
CLOSE ProblemGroupStringCursor 
DEALLOCATE ProblemGroupStringCursor   

Select @ProblemGroupStringCombined = SUBSTRING (@ProblemGroupStringCombined,1,Len(@ProblemGroupStringCombined)-1)

INSERT INTO #tblProblemString VALUES (@ProjectID, @ProblemString, @ProblemGroupStringCombined)
Join our community to see this answer!
Unlock 1 Answer and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros