My data looks like
ID MyText
1 some text; some more text
2 text again; even more text
How can I update MyText to drop everything after the semi-colon and including the semi colon, so I'm left with the following:
ID MyText
1 some text
2 text again
I've looked at SQL Server Replace, but can't think of a viable way of checking for the ";"
For the times when some fields have a ";" and some do not you can also add a semi-colon to the field and use the same method described.
For situations when I need to replace or match(find) something against string I prefer using regular expressions.
Since, the regular expressions are not fully supported in
T-SQL
you can implement them usingCLR
functions. Furthermore, you do not need anyC#
orCLR
knowledge at all as all you need is already available in the MSDN String Utility Functions Sample.In your case, the solution using regular expressions is:
But implementing such function in your database is going to help you solving more complex issues at all.
The example below shows how to deploy only the
[dbo].[RegexReplace]
function, but I will recommend to you to deploy the wholeString Utility
class.Enabling CLR Integration. Execute the following Transact-SQL commands:
Bulding the code (or creating the
.dll
). Generraly, you can do this using the Visual Studio or .NET Framework command prompt (as it is shown in the article), but I prefer to use visual studio.create new class library project:
copy and paste the following code in the
Class1.cs
file:build the solution and get the path to the created
.dll
file:replace the path to the
.dll
file in the followingT-SQL
statements and execute them:That's it. Test your function:
Use
CHARINDEX
to find the ";". Then useSUBSTRING
to just return the part before the ";".Use LEFT combined with CHARINDEX:
Note that the WHERE clause skips updating rows in which there is no semicolon.
Here is some code to verify the SQL above works:
I get the following results:
Could use
CASE WHEN
to leave those with no ';' alone.