SQL Serverreplace,删除一定的字符后
我的数据看起来像
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 ,但不能想到一个可行的方式来检查“;”
使用LEFT结合CHARINDEX:
 UPDATE MyTable SET MyText = LEFT(MyText, CHARINDEX(';', MyText) - 1) WHERE CHARINDEX(';', MyText) > 0 
请注意,WHERE子句将跳过没有分号的更新行。
以下是validation上述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) 
对于某些领域有“”的时代, 有些不你也可以在字段中添加一个分号,并使用相同的方法描述。
 SET MyText = LEFT(MyText+';', CHARINDEX(';',MyText+';')-1) 
 可以使用CASE WHEN离开那些没有';' 单独。 
  SELECT CASE WHEN CHARINDEX(';', MyText) > 0 THEN LEFT(MyText, CHARINDEX(';', MyText)-1) ELSE MyText END FROM MyTable 
 使用CHARINDEX来查找“;”。 然后使用SUBSTRING返回“;”之前的部分。 
 UPDATE MyTable SET MyText = SUBSTRING(MyText, 1, CHARINDEX(';', MyText) - 1) WHERE CHARINDEX(';', MyText) > 0 
对于需要replace或匹配(查找)string的情况,我更喜欢使用正则expression式。
 由于正则expression式在T-SQL中并不完全支持,所以可以使用CLR函数来实现它们。 此外,您根本不需要任何C#或CLR知识,因为您只需要MSDN string实用程序函数示例中的所有信息 。 
在你的情况下,使用正则expression式的解决scheme是:
 SELECT [dbo].[RegexReplace] ([MyColumn], '(;.*)', '') FROM [dbo].[MyTable] 
但是在数据库中实现这样的function将会帮助你解决更复杂的问题。
 下面的例子显示了如何只部署[dbo].[RegexReplace]函数,但是我build议你部署整个String Utility类。 
- 
启用CLR集成。 执行以下Transact-SQL命令: sp_configure 'clr enabled', 1 GO RECONFIGURE GO
- 
构build代码(或创build .dll)。 一般来说,您可以使用Visual Studio或.NET Framework命令提示符(如文章中所示)执行此操作,但我更愿意使用Visual Studio。- 
创build新的类库项目:  
- 
将下面的代码复制并粘贴到 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); } }
- 
构build解决scheme并获取创build的 .dll文件的path: 
- 
replace下面的 T-SQL语句中的.dll文件的path并执行它们: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
- 
而已。 testing你的function: 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
 
-