SQL Server替换,删除所有特定字符后

时间:2009-11-03 15:25:07

标签: sql sql-server tsql replace

我的数据看起来像

ID    MyText
1     some text; some more text
2     text again; even more text

如何更新MyText以删除分号后的所有内容并包括半冒号,所以我留下以下内容:

ID    MyText
1     some text
2     text again

我看过SQL Server Replace,但想不出一种检查“;”的可行方法

6 个答案:

答案 0 :(得分:89)

使用LEFT结合CHARINDEX:

UPDATE MyTable
SET MyText = LEFT(MyText, CHARINDEX(';', MyText) - 1)
WHERE CHARINDEX(';', MyText) > 0

请注意,WHERE子句会跳过更新没有分号的行。

以下是验证上述SQL的一些代码:

declare @MyTable table ([id] int primary key clustered, MyText varchar(100))
insert into @MyTable ([id], MyText)
select 1, 'some text; some more text'
union all select 2, 'text again; even more text'
union all select 3, 'text without a semicolon'
union all select 4, null -- test NULLs
union all select 5, '' -- test empty string
union all select 6, 'test 3 semicolons; second part; third part;'
union all select 7, ';' -- test semicolon by itself    

UPDATE @MyTable
SET MyText = LEFT(MyText, CHARINDEX(';', MyText) - 1)
WHERE CHARINDEX(';', MyText) > 0

select * from @MyTable

我得到以下结果:

id MyText
-- -------------------------
1  some text
2  text again
3  text without a semicolon
4  NULL
5        (empty string)
6  test 3 semicolons
7        (empty string)

答案 1 :(得分:18)

对于某些字段有“;”的时间有些人不能在字段中添加分号并使用相同的方法。

SET MyText = LEFT(MyText+';', CHARINDEX(';',MyText+';')-1)

答案 2 :(得分:11)

可以使用CASE WHEN留下没有';'的人单独。

    SELECT
    CASE WHEN CHARINDEX(';', MyText) > 0 THEN
    LEFT(MyText, CHARINDEX(';', MyText)-1) ELSE
    MyText END
    FROM MyTable

答案 3 :(得分:3)

使用CHARINDEX查找“;”。然后使用SUBSTRING返回“;”之前的部分。

答案 4 :(得分:2)

UPDATE MyTable
   SET MyText = SUBSTRING(MyText, 1, CHARINDEX(';', MyText) - 1)
 WHERE CHARINDEX(';', MyText) > 0 

答案 5 :(得分:1)

对于需要替换或匹配(找到)字符串的情况,我更喜欢使用正则表达式。

由于T-SQL中不完全支持正则表达式,您可以使用CLR函数实现它们。此外,您根本不需要任何C#CLR知识,因为MSDN String Utility Functions Sample中已经提供了所有您需要的信息。

在您的情况下,使用正则表达式的解决方案是:

SELECT [dbo].[RegexReplace] ([MyColumn], '(;.*)', '')
FROM [dbo].[MyTable]

但是在数据库中实现这样的功能将帮助您解决更复杂的问题。


以下示例显示了如何仅部署[dbo].[RegexReplace]函数,但我建议您部署整个String Utility类。

  1. 启用CLR集成。执行以下Transact-SQL命令:

    sp_configure 'clr enabled', 1
    GO
    RECONFIGURE
    GO  
    
  2. 建造代码(或创建.dll)。通常,您可以使用Visual Studio或.NET Framework命令提示符(如文章中所示)执行此操作,但我更喜欢使用visual studio。

    • 创建新的类库项目:

      enter image description here

    • 将以下代码复制并粘贴到Class1.cs文件中:

      using System;
      using System.IO;
      using System.Data.SqlTypes;
      using System.Text.RegularExpressions;
      using Microsoft.SqlServer.Server;
      
      public sealed class RegularExpression
      {
          public static string Replace(SqlString sqlInput, SqlString sqlPattern, SqlString sqlReplacement)
          {
              string input = (sqlInput.IsNull) ? string.Empty : sqlInput.Value;
              string pattern = (sqlPattern.IsNull) ? string.Empty : sqlPattern.Value;
              string replacement = (sqlReplacement.IsNull) ? string.Empty : sqlReplacement.Value;
              return Regex.Replace(input, pattern, replacement);
          }
      }
      
    • 构建解决方案并获取创建的.dll文件的路径:

      enter image description here

    • 在以下.dll语句中替换T-SQL文件的路径并执行它们:

      IF OBJECT_ID(N'RegexReplace', N'FS') is not null
      DROP Function RegexReplace;
      GO
      
      IF EXISTS (SELECT * FROM sys.assemblies WHERE [name] = 'StringUtils')
      DROP ASSEMBLY StringUtils;
      GO
      
      DECLARE @SamplePath nvarchar(1024)
      -- You will need to modify the value of the this variable if you have installed the sample someplace other than the default location.
      Set @SamplePath = 'C:\Users\gotqn\Desktop\StringUtils\StringUtils\StringUtils\bin\Debug\'
      CREATE ASSEMBLY [StringUtils] 
      FROM @SamplePath + 'StringUtils.dll'
      WITH permission_set = Safe;
      GO
      
      
      CREATE FUNCTION [RegexReplace] (@input nvarchar(max), @pattern nvarchar(max), @replacement nvarchar(max))
      RETURNS nvarchar(max)
      AS EXTERNAL NAME [StringUtils].[RegularExpression].[Replace]
      GO
      
    • 就是这样。测试你的功能:

      declare @MyTable table ([id] int primary key clustered, MyText varchar(100))
      insert into @MyTable ([id], MyText)
      select 1, 'some text; some more text'
      union all select 2, 'text again; even more text'
      union all select 3, 'text without a semicolon'
      union all select 4, null -- test NULLs
      union all select 5, '' -- test empty string
      union all select 6, 'test 3 semicolons; second part; third part'
      union all select 7, ';' -- test semicolon by itself    
      
      SELECT [dbo].[RegexReplace] ([MyText], '(;.*)', '')
      FROM @MyTable
      
      select * from @MyTable
      
相关问题