.net SQL提取子字符串

.net SQL提取子字符串,.net,sql,string,.net,Sql,String,我在提取SQL查询结果中的子字符串时遇到问题 情况如下:我有一列包含以下格式的字符串:“ax123456uhba”、“ax54232hrg”、“ax274895rt”、“ax938477ed1”、“ax73662633wnn2” 我需要提取前跟字母的数字字符串。但是,偶尔在后面的字符串中有一个我不需要的数字。尾随字符的长度不是静态的,所以我不能只做一个简单的子字符串函数 我不一定要完整的代码,只要在可能的情况下朝着正确的方向努力就行了 提前感谢您的帮助。如果您使用的是.NET,您可以使用正则表达

我在提取SQL查询结果中的子字符串时遇到问题

情况如下:我有一列包含以下格式的字符串:“ax123456uhba”、“ax54232hrg”、“ax274895rt”、“ax938477ed1”、“ax73662633wnn2”

我需要提取前跟字母的数字字符串。但是,偶尔在后面的字符串中有一个我不需要的数字。尾随字符的长度不是静态的,所以我不能只做一个简单的子字符串函数

我不一定要完整的代码,只要在可能的情况下朝着正确的方向努力就行了


提前感谢您的帮助。

如果您使用的是.NET,您可以使用正则表达式获取它:

var input = "ax938477ed1";
var reg = new Regex("[0-9]+");
var match = reg.Match(input);
int number = -1;
if (match.Success)
    number = Convert.ToInt32(match.Groups[0].Value);

这将存储938477的数字。

如果您使用的是.NET,您可以使用正则表达式获取它:

var input = "ax938477ed1";
var reg = new Regex("[0-9]+");
var match = reg.Match(input);
int number = -1;
if (match.Success)
    number = Convert.ToInt32(match.Groups[0].Value);

这将存储938477的数字。

使用正则表达式可能是最简单的

取决于数据库-有些具有正则表达式函数-(SQL Server看起来可以将其添加到服务器


否则,您可以使用like来减少查询。Sybase允许像“%[0-9]&”这样的x查找包含数字的行,然后在客户端中使用正则表达式。

可能使用正则表达式是最简单的

取决于数据库-有些具有正则表达式函数-(SQL Server看起来可以将其添加到服务器


否则,您可以使用like来减少查询。Sybase允许类似于“%[0-9]&”的x查找包含数字的行,然后在客户端中使用正则表达式。

看起来PATINDEX就是您需要的

返回在字符串正则表达式中找到的模式的第一个索引 看到这个->

这里复制的代码用于从字符串中删除字母数字字符-将其更改为从字符串中删除第一个连续的数字序列不需要太长时间

CREATE FUNCTION dbo.UDF_ParseAlphaChars
(
@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @IncorrectCharLoc SMALLINT
    SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)

    WHILE @IncorrectCharLoc > 0
    BEGIN
        SET @string = STUFF(@string, @IncorrectCharLoc, 1, '')
        SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)
    END

    SET @string = @string

    RETURN @string
END
GO

看起来你需要的是PATINDEX

返回在字符串正则表达式中找到的模式的第一个索引 看到这个->

这里复制的代码用于从字符串中删除字母数字字符-将其更改为从字符串中删除第一个连续的数字序列不需要太长时间

CREATE FUNCTION dbo.UDF_ParseAlphaChars
(
@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @IncorrectCharLoc SMALLINT
    SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)

    WHILE @IncorrectCharLoc > 0
    BEGIN
        SET @string = STUFF(@string, @IncorrectCharLoc, 1, '')
        SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)
    END

    SET @string = @string

    RETURN @string
END
GO

我同意为此使用正则表达式,假设您在SQL 2005或2008上可以使用CLR。以下是一些在SQL Server中使用正则表达式的UDF代码,这些代码应该会有所帮助:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.SqlServer.Server

Partial Public Class UserDefinedFunctions

    <Microsoft.SqlServer.Server.SqlFunction()>
    Public Shared Function IsRegexMatch(ByVal input As SqlString, ByVal pattern As SqlString) As SqlBoolean
        If input.IsNull OrElse pattern.IsNull Then Return SqlBoolean.Null
        Return Regex.IsMatch(input.Value, pattern.Value, RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.Multiline)
    End Function

    <Microsoft.SqlServer.Server.SqlFunction()>
    Public Shared Function RegexReplace(ByVal input As SqlString, ByVal pattern As SqlString, ByVal replacement As SqlString) As SqlString
        If input.IsNull OrElse pattern.IsNull OrElse replacement.IsNull Then Return SqlString.Null
        Return Regex.Replace(input.Value, pattern.Value, replacement.Value, RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.Multiline)
    End Function
End Class
导入系统
导入系统数据
导入System.Data.SqlClient
导入System.Data.SqlTypes
导入系统文本
导入System.Text.RegularExpressions
导入Microsoft.SqlServer.Server
部分公共类UserDefinedFunctions
公共共享函数IsRegexMatch(ByVal输入为SqlString,ByVal模式为SqlString)为SqlBoolean
如果input.IsNull或lse pattern.IsNull,则返回SqlBoolean.Null
返回Regex.IsMatch(input.Value、pattern.Value、RegexOptions.IgnorePatternWhitespace或RegexOptions.Singleline或RegexOptions.Multiline)
端函数
公共共享函数RegexReplace(ByVal输入为SqlString,ByVal模式为SqlString,ByVal替换为SqlString)作为SqlString
如果input.IsNull或lse pattern.IsNull或lse replacement.IsNull,则返回SqlString.Null
返回Regex.Replace(input.Value、pattern.Value、replacement.Value、RegexOptions.IgnorePatternWhitespace或RegexOptions.Singleline或RegexOptions.Multiline)
端函数
末级

我同意使用正则表达式,假设您在SQL 2005或2008上可以使用CLR。以下是一些在SQL Server中使用正则表达式的UDF代码,应该会有所帮助:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.SqlServer.Server

Partial Public Class UserDefinedFunctions

    <Microsoft.SqlServer.Server.SqlFunction()>
    Public Shared Function IsRegexMatch(ByVal input As SqlString, ByVal pattern As SqlString) As SqlBoolean
        If input.IsNull OrElse pattern.IsNull Then Return SqlBoolean.Null
        Return Regex.IsMatch(input.Value, pattern.Value, RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.Multiline)
    End Function

    <Microsoft.SqlServer.Server.SqlFunction()>
    Public Shared Function RegexReplace(ByVal input As SqlString, ByVal pattern As SqlString, ByVal replacement As SqlString) As SqlString
        If input.IsNull OrElse pattern.IsNull OrElse replacement.IsNull Then Return SqlString.Null
        Return Regex.Replace(input.Value, pattern.Value, replacement.Value, RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.Multiline)
    End Function
End Class
导入系统
导入系统数据
导入System.Data.SqlClient
导入System.Data.SqlTypes
导入系统文本
导入System.Text.RegularExpressions
导入Microsoft.SqlServer.Server
部分公共类UserDefinedFunctions
公共共享函数IsRegexMatch(ByVal输入为SqlString,ByVal模式为SqlString)为SqlBoolean
如果input.IsNull或lse pattern.IsNull,则返回SqlBoolean.Null
返回Regex.IsMatch(input.Value、pattern.Value、RegexOptions.IgnorePatternWhitespace或RegexOptions.Singleline或RegexOptions.Multiline)
端函数
公共共享函数RegexReplace(ByVal输入为SqlString,ByVal模式为SqlString,ByVal替换为SqlString)作为SqlString
如果input.IsNull或lse pattern.IsNull或lse replacement.IsNull,则返回SqlString.Null
返回Regex.Replace(input.Value、pattern.Value、replacement.Value、RegexOptions.IgnorePatternWhitespace或RegexOptions.Singleline或RegexOptions.Multiline)
端函数
末级

您是用SQL还是.NET处理这个子字符串?现在用SQL处理..NET不在可能的范围内。您是用SQL还是.NET处理这个子字符串?现在用SQL处理..NET不在可能的范围内。很好的发现!非常感谢!记录在案,这个UDF将删除所有可能的子字符串tters。它将把“a9d1”翻译成91。很好的发现!非常感谢!为了记录,这个UDF将删除所有字母。它将把“a9d1”翻译成91。