I have the following SQL CLR C# UDF:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Text;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString clrFn_GetDigits(string theWord)
{
if (theWord == null) { theWord = ""; }
string newWord = "";
char[] KeepArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', '/', '-', ' '};
foreach (char thischar in theWord)
{
foreach (char keepchar in KeepArray)
{
if (keepchar == thischar)
{
newWord += thischar;
}
}
}
return (SqlString)(newWord.Trim());
}
}
This works great so far except for addresses like the below:
141A, Some Street Avenue
4b, St Georges Street
16E Test Avenue
I want my function to return 141A, 4b and 16E
Any ideas?
I have not tested the below code, but something along those lines, some error checking will need to be in place to ensure the converts do not fail but this solution gives you what you need
char[] KeepArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', '/', '-', ' ' };
foreach (char thischar in theWord) {
if (KeepArray.Contains(thischar)) {
newWord += thischar;
}
else if (Char.IsLetter(thischar) && newWord.Length > 0){
try {
if (Char.IsDigit((Convert.ToChar(newWord.Substring(newWord.Length - 1, 1))))) {
newWord += thischar;
}
}
catch {
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.