簡體   English   中英

SQL CLR C#用戶定義的函數-從地址獲取房屋或公寓編號

[英]SQL CLR C# User Defined Function - Get house or flat number from address

我有以下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 {

                }
            }

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM