Ric*_*ich 9 sql t-sql sql-server
我想从一些varchars中拉出浮点数,使用PATINDEX()来发现它们.我知道在每个varchar字符串中,我只对存在的第一个浮点感兴趣,但它们可能有不同的长度.
例如
'some text 456.09 other text'
'even more text 98273.453 la la la'
Run Code Online (Sandbox Code Playgroud)
我通常会将这些与正则表达式匹配
"[0-9]+[.][0-9]+"
Run Code Online (Sandbox Code Playgroud)
但是,我找不到PATINDEX接受的+运算符的等价物.所以他们需要(分别)匹配:
'[0-9][0-9][0-9].[0-9][0-9]' and '[0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9]'
Run Code Online (Sandbox Code Playgroud)
有没有办法将这两个示例varchars与一个有效的PATINDEX模式匹配?
G M*_*ros 12
我刚才在博客上写了这篇文章. 使用SQL Server提取数字
Declare @Temp Table(Data VarChar(100))
Insert Into @Temp Values('some text 456.09 other text')
Insert Into @Temp Values('even more text 98273.453 la la la')
Insert Into @Temp Values('There are no numbers in this one')
Select Left(
SubString(Data, PatIndex('%[0-9.-]%', Data), 8000),
PatIndex('%[^0-9.-]%', SubString(Data, PatIndex('%[0-9.-]%', Data), 8000) + 'X')-1)
From @Temp
Run Code Online (Sandbox Code Playgroud)