【问题标题】:SQL CLR C# User Defined Function - Get house or flat number from addressSQL CLR C# 用户定义函数 - 从地址获取房屋或公寓号码
【发布时间】:2013-02-11 10:48:28
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 你需要一些逻辑来检查下一个字符

标签: c# sql clr user-defined-functions


【解决方案1】:

我没有测试下面的代码,但是按照这些思路,需要进行一些错误检查以确保转换不会失败,但是这个解决方案可以满足您的需求

    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 {

                }
            }

        }

【讨论】:

  • 好东西 CR41G14!像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多