Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005
    Posts
    31

    Unanswered: ouput of difference in two Strings

    Hi, I need an ouput of difference in two Strings

    Declare @StringA varchar(100), @StringB varchar(100)

    @StringA = 'A,B,C'
    @StringB = 'A,C,C'

    The values are seperated in the strings by ','

    Since B value in first string changed to C value in the second string
    I want to return the difference between the two strings as 'B,C'

    Can anyone help me with this.

    Thanks

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Well, you could write a user-defined function that accepts two strings and steps through them each one character at a time to detect differences. But this is a really goofy setup. Can't you normalize your data? Storing data in comma-delimited strings is right up there at the top of the list of DBA cardinal sins. In standard 3rd-normal form this request would be very easy.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    Plus depending on what you mean by "difference", your solution could also be different.

    A comma-delimited list is not really a "string" that you can detect differences in...I mean, it is, but not the way you are using it (as I understand your question, anyway).

    I mean, is "A,B,C" the same as "B,C,A" or "C,B,A"??? Or are those supposed to be "different" strings?

    There are quite a few procedures, functions, and other code examples on the 'net and in the dbforums archives about sucking elements out of CSV lists...but then you still have to deal with the now free-floating elements (again, does order matter?)

    Yeah, like The Blind One says...normalize your data...or at least maybe create a temp table from the strings? Naaahhh...normalize your data.

    Go ahead, we'll wait
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •