如何比较两个字符串结果显示旧列更改文本和新更改文本

时间:2017-02-27 12:14:00

标签: sql-server sql-server-2008 sql-server-2008-r2

下面的函数告诉我已更改的单词,但我希望这些列名也返回已更改的单词。如何修改此函数以告诉我已更改的字符串列名..

  

像=> ' company_name:绿色市场'更改为' company_name:绿色超级市场'

我是SQL SERVER的新手请帮助我

     SELECT dbo.CharDiff('{
        "_id" : ObjectId("5877c25b7c32d41827306d63"),
        "salt" : "ezmMjMn4A2cEb7ZZcc2cXQ==",
        "company_name" : "green Market",
        "designation" : "manager",
        "email" : "abc@gmail.com",
        "fcm_token" : "eqTXWSrYd",
        "full_name" : "John",
        "mobile_number" : "123456789",
        "password" : "Q2uQlO",
        "user_type" : "Customer",
        "is_active" : true,
        "is_approved" : true,    
        "role" : "user"
    }','{
        "_id" : ObjectId("5877c25b7c32d41827306d63"),
        "salt" : "ezmMjMn4A2cEb7ZZcc2cXQ==",
        "company_name" : "green super Market",
        "designation" : "manager",
        "email" : "abc@gmail.com",
        "fcm_token" : "eqTXWSrYd",
        "full_name" : "John",
        "mobile_number" : "123456789",
        "password" : "Q2uQlO",
        "user_type" : "Customer",
        "is_active" : true,
        "is_approved" : true,    
        "role" : "user"
    }');

    Alter function dbo.CharDiff(@oldtext nvarchar(max), @newtext nvarchar(max))

 returns nvarchar(max) as begin
   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 @oldtext <> @newtext
   begin
      -- find the first non-matching character
      while substring(@oldtext, @c, 1) = substring(@newtext, @c, 1) set @c = @c + 1;

      -- remove up to the first non-matching character for both strings
      set @newtext = RIGHT(@newtext, len(@newtext) - @c + 1);  
      set @oldtext = RIGHT(@oldtext, len(@oldtext) - @c + 1);

      -- if the strings are different length then reverse them so you can start at 1 
      set @oldtext=reverse(@oldtext);  
      set @newtext=reverse(@newtext);
      set @c = 0;  -- reset the counter

      while substring(@oldtext, @c, 1) = substring(@newtext, @c, 1) set @c = @c + 1;
      set @newtext = RIGHT(@newtext, len(@newtext) - @c +2);
      set @oldtext = RIGHT(@oldtext, len(@oldtext) - @c +2);

      set @oldtext=ltrim(rtrim(reverse(@oldtext)));
      set @newtext=ltrim(rtrim(reverse(@newtext)));

      set @retval = '[' + @oldtext + '] to [' + @newtext + ']';
   end;

   return @retval;
end

1 个答案:

答案 0 :(得分:0)

你应该把它变成一个表值函数。有关示例,请参阅here