SQL CLR C#用户定义函数-从地址获取房屋或公寓号
本文关键字:获取 公寓 地址 CLR 用户 定义 函数 SQL | 更新日期: 2023-09-27 17:58:43
我有以下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());
}
}
到目前为止,除了下面这样的地址外,这一切都很好:
141A, Some Street Avenue
4b, St Georges Street
16E Test Avenue
我希望我的函数返回141A、4b和16E
有什么想法吗?
我还没有测试下面的代码,但在这些方面,需要进行一些错误检查,以确保转换不会失败,但这个解决方案为您提供了所需的
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 {
}
}
}