0

I wasn't sure what to title this question, but feel free to edit it if you have a better idea.

Let's say I have data that looks like this:

Column A                         Column B
John, Sally, Cindy               John, Sally, Cindy, Steve
John, Cindy                      John, Sally, Cindy
Sally, Cindy                     Sally, Cindy
Sally, Steve                     John, Sally, Steve

What I would like to do is find the differences. I want to see what exists in column B that are not in column A, so that I have a column that looks like this:

Column C
Steve
Sally
''
John

Any suggestions for approaching this?

Edit #1:

The table is not stored like this, the table does not have multiple values per cell; however, I am sending a report from a SQL query and the assignment is to show the differences with columns a and b having multiple values like this.

This is what my SQL query currently looks like in the results so far.

Edit #2:

There are not multiple values per record/column intersection in the table. To make the report easier to view for the end user, I placed multiple values in the intersection, in my SQL Query Results, so show what is there and what is not there.

I am trying to create column C to show the differences.

Edit #3:

Column A comes from one data source Column B comes from another data source.

A and B are not subsets of each other, I am simply taking 2 columns and trying to find the differences in an easier way.

  • 1
    See [this post](https://stackoverflow.com/questions/40280782/how-to-find-diff-between-two-string-in-sql) – BJones Dec 23 '19 at 16:05
  • Just curious. Is John Sally in column B missing a comma or is that intentional ? – John Cappelletti Dec 23 '19 at 16:15
  • They were missing a comma by mistake, I fixed it. Sorry :) – Chicken Sandwich No Pickles Dec 23 '19 at 17:09
  • If "the table does not have multiple values per cell" then it isn't clear what you're asking for. The difference between `'John, Cindy'` and `'John, Sally, Cindy'` is `', Sally'` or `'Sally, '` since there is only a string, not a list. Is `[Column A]` always a subset of `[Column B]` or could it have a _cell_ (?) with a longer string? What is the difference between `'John, Sally'` and `'Sally, John'`, or are the values (that don't exist) always in alphabetical order? `'John, Cindy'` and `'Johnson, Mindy'` have nothing in common with a result of ... ? – HABO Dec 23 '19 at 17:30

2 Answers2

2

Not clear if you want multiple differences aggregated into one delimited cell.

Example

Declare @YourTable table (ColA varchar(150),ColB varchar(150))
Insert Into @YourTable values 
('John, Sally, Cindy','John, Sally, Cindy, Steve'),
('John, Cindy','John, Sally, Cindy'),
('Sally, Cindy','Sally, Cindy'),
('Sally, Steve','John, Sally, Steve')

Select A.*
      ,B.*
 From  @YourTable A
 Outer Apply (
                Select Diff=value
                 From (
                        Select value=ltrim(rtrim(value)) From string_split(ColA,',')
                        Union All
                        Select value=ltrim(rtrim(value)) From string_split(ColB,',')
                      ) B1
                  Group By Value
                  Having count(*)=1
             ) B

Returns

ColA                ColB                        Diff
John, Sally, Cindy  John, Sally, Cindy, Steve   Steve
John, Cindy         John, Sally, Cindy          Sally
Sally, Cindy        Sally, Cindy    
Sally, Steve        John, Sally, Steve          John
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
1

another way is by using function

create function dbo.getdiff(@colA varchar(100), @colB varchar(100))
returns varchar(100)
as
begin
    return (select stuff((select ',' + value from(
    select trim(value) value FROM string_split(@colB, ',')
    EXCEPT SELECT trim(value) FROM string_split(@colA, ',')) x  for xml path('')),1,1,''))
end

And get the desired result

select cola, colb, dbo.getdiff(cola, colb) diff from summarytable
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25