Professional Web Applications Themes

Difference between strings - Microsoft SQL / MS SQL Server

In ss. 7.0 Is there a function available that will return the differences between two strings? e.g. The difference between 'This is a test' and 'This is Test' would return 'a'. TIA, Jordan...

  1. #1

    Default Difference between strings

    In ss. 7.0

    Is there a function available that will return the
    differences between two strings?

    e.g.
    The difference between 'This is a test' and 'This is Test'
    would return 'a'.

    TIA, Jordan
    JMNUSS Guest

  2. #2

    Default Re: Difference between strings

    No there is not. You'll have to write a stored procedure for that
    as there are no udf's on sql 7.0.

    --
    Dean Savovic
    [url]www.teched.hr[/url]


    "JMNUSS" <replyinnghome.com> wrote in message
    news:00a001c3449f$423d0550$a301280aphx.gbl...
    > In ss. 7.0
    >
    > Is there a function available that will return the
    > differences between two strings?
    >
    > e.g.
    > The difference between 'This is a test' and 'This is Test'
    > would return 'a'.
    >
    > TIA, Jordan

    Dean Savovic Guest

  3. #3

    Default Re: Difference between strings

    I am pretty sure there isnt. This is a very complex test, sounds the same as
    BeyondCompare2 comparing two text files and showing you text thats been
    deleted from file 2, text thats been added to file2. Needs to do a lot of
    clever work to try and synch the files as closely as posible.

    I am sure you could buy ( or write ) this though , just not sure where from.

    Chris Brooksbank


    "JMNUSS" <replyinnghome.com> wrote in message
    news:00a001c3449f$423d0550$a301280aphx.gbl...
    > In ss. 7.0
    >
    > Is there a function available that will return the
    > differences between two strings?
    >
    > e.g.
    > The difference between 'This is a test' and 'This is Test'
    > would return 'a'.
    >
    > TIA, Jordan

    Chris Brooksbank Guest

  4. #4

    Default Re: Difference between strings

    Afraid not. You gotta p the strings manually.

    --
    -oj
    RAC v2.2 & QALite!
    [url]http://www.rac4sql.net[/url]


    "JMNUSS" <replyinnghome.com> wrote in message
    news:00a001c3449f$423d0550$a301280aphx.gbl...
    > In ss. 7.0
    >
    > Is there a function available that will return the
    > differences between two strings?
    >
    > e.g.
    > The difference between 'This is a test' and 'This is Test'
    > would return 'a'.
    >
    > TIA, Jordan

    oj Guest

  5. #5

    Default Re: Difference between strings

    That's what I was afraid of, thanks anyway.

    >-----Original Message-----
    >No there is not. You'll have to write a stored procedure
    for that
    >as there are no udf's on sql 7.0.
    >
    >--
    >Dean Savovic
    >[url]www.teched.hr[/url]
    >
    >
    >"JMNUSS" <replyinnghome.com> wrote in message
    >news:00a001c3449f$423d0550$a301280aphx.gbl...
    >> In ss. 7.0
    >>
    >> Is there a function available that will return the
    >> differences between two strings?
    >>
    >> e.g.
    >> The difference between 'This is a test' and 'This is
    Test'
    >> would return 'a'.
    >>
    >> TIA, Jordan
    >
    >
    >.
    >
    JMNUSS Guest

  6. #6

    Default Re: Difference between strings

    Use a programming language for such requirements.

    If only the first difference needs to be displayed, for fun, you can use a
    number table & do:

    SELECT TOP 1 SUBSTRING(Str1, Nbr, 1) AS "diff"
    FROM Numbers
    WHERE SUBSTRING(Str1, Nbr, 1) <> SUBSTRING(Str2, Nbr, 1)
    ORDER BY Nbr;

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Sen Guest

  7. #7

    Default Re: Difference between strings

    "Anith Sen" wrote in message
    ..
    > If only the first difference needs to be displayed, for fun, you can use a
    > number table & do:
    ..
    ..

    *For fun*.
    I've always found that a strange sort of disclaimer:)
    Does its absence mean your *really* serious?:)

    This may call for another limerick:)

    Hope your doing well!


    For fun and laughs or if your about to have a heart attack
    check out RAC v2.2 and QALite
    [url]www.rac4sql.net[/url]


    :~)


    Groucho Guest

  8. #8

    Default Re: Difference between strings

    >> Does its absence mean your *really* serious?:) ... This may call for
    another limerick:) <<

    :-)...now, you are getting serious, aren't you? Go ahead, pour your poetry
    ....

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Sen Guest

  9. #9

    Default Re: Difference between strings

    No.

    However, Cecil Lew once posted code to calculate the difference. The
    relevant thread can be found at
    [url]http://groups.google.com/groups?hl=nl&lr=&ie=UTF-8&threadm=c070879a.0208130814.2b7046ce%40posting.g oogle.com&rnum=1&prev=/groups%3Fhl%3Dnl%26lr%3D%26ie%3DUTF-8%26selm%3Dc070879a.0208130814.2b7046ce%2540postin g.google.com[/url]

    Hope this helps,
    Gert-Jan


    JMNUSS wrote:
    >
    > In ss. 7.0
    >
    > Is there a function available that will return the
    > differences between two strings?
    >
    > e.g.
    > The difference between 'This is a test' and 'This is Test'
    > would return 'a'.
    >
    > TIA, Jordan
    Gert-Jan Strik Guest

  10. #10

    Default Re: Difference between strings

    "Anith Sen" <anithbizdatasolutions.com> wrote in message
    news:O#Jb09KRDHA.1072TK2MSFTNGP10.phx.gbl...
    > :-)...now, you are getting serious, aren't you? Go ahead, pour your poetry
    There was once a group that was bored,
    Until they found a language that they thought soared,
    And it was never too much to show there success,
    After seeing an attempt that they thought a mess,
    They thought it of such purity,
    That it brought them security,
    But beneath their blaze,
    Lurked a germ of haze,
    So in their confusion,
    There was always the illusion,
    That what they were doing was as it should have been done,
    Until they discovered it was all in fun.



    Groucho Guest

  11. #11

    Default Re: Difference between strings

    :-) You are good... P.Diddy can rap this too!

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Sen Guest

  12. #12

    Default Re: Difference between strings

    This code will only remove matching characters from the beginning and end. If there are more matching in the middle of the string this code will not catch it. This should work for what I need it for. I figured since I couldn't find any code that I would share with the group.

    Code:
    alter function GetDiff(before nvarchar(max), after nvarchar(max)) returns nvarchar(max) as begin
    /*
    print '1) middle ' + dbo.GetDiff('can you spot a difference?', 'can you spot the difference?');
    print '2) middle ' + dbo.GetDiff('can you spot the difference?', 'can you spot a difference?');
    print '3) beginning ' + dbo.GetDiff('where do the changes start?', 'in what part do any changes start?');
    print '4) beginning ' + dbo.GetDiff('in what part do any changes start?', 'where do the changes start?');
    print '5) end ' + dbo.GetDiff('sometimes you just can''t get rid of a bomb', 'sometimes you just can''t get rid of a hickey');
    print '6) end ' + dbo.GetDiff('sometimes you just can''t get rid of a hickey', 'sometimes you just can''t get rid of a bomb');
    print '7) same ' + dbo.GetDiff('this is an amazing test', 'this is an amazing test');
    */
       declare retval as nvarchar(max); set retval = '';
       declare c as integer; set c = 0; -- counter
       declare t as nvarchar(max);
    
       -- if the strings match then there is no difference
       if before <> after
       begin
          -- find the first non-matching character
          while substring(before, c, 1) = substring(after, c, 1) set c = c + 1;
    
    	  -- remove up to the first non-matching character for both strings
    	  set after = RIGHT(after, len(after) - c + 1);  
    	  set before = RIGHT(before, len(before) - c + 1);
    
    	  -- if the strings are different length then reverse them so you can start at 1 
          set before=reverse(before);  
    	  set after=reverse(after);
    	  set c = 0;  -- reset the counter
    
    	  while substring(before, c, 1) = substring(after, c, 1) set c = c + 1;
    	  set after = RIGHT(after, len(after) - c +2);
    	  set before = RIGHT(before, len(before) - c +2);
    
          set before=ltrim(rtrim(reverse(before)));
    	  set after=ltrim(rtrim(reverse(after)));
    
    	  set retval = '[' + before + '] to [' + after + ']';
       end;
       
       return retval;
    end
    DKermott is offline Junior Member
    Join Date
    Feb 2013
    Location
    Beloit, Wisconsin, USA
    Posts
    1

Similar Threads

  1. Is there a difference???
    By evil-bear in forum Coldfusion Database Access
    Replies: 1
    Last Post: January 19th, 04:30 PM
  2. Why is there a difference??
    By Steve Grosz in forum Macromedia ColdFusion
    Replies: 11
    Last Post: June 10th, 03:50 PM
  3. Extracting strings delimited by other strings
    By Scott Bass in forum PERL Modules
    Replies: 2
    Last Post: May 7th, 02:26 AM
  4. Difference between ASP & PHP
    By Peter van Rijt in forum PHP Development
    Replies: 11
    Last Post: January 21st, 11:12 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139