SQL从字符串列中提取多个数值

时间:2016-06-09 12:23:18

标签: sql sql-server string numeric

我阅读并使用了本文中建议的功能:

http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/

它工作得很好但实际上我需要两列中的结果(我希望两个数字应该用原始字符串中的空格和/ char分隔)

让我们说" nfsdklj6 / 3 ddfsdf"应该是6 | 3 " nfsdklj45 / 100 ddfsdf"应该是45 | 100

我是一名新手sql程序员,所以任何帮助或提示都会受到赞赏, 感谢

1 个答案:

答案 0 :(得分:2)

如果您使用的是上述的udf

Declare @Table table (MyString varchar(25))
Insert into @Table (MyString) values 
('nfsdklj6 / 3 ddfsdf'),
('nfsdklj45 / 100 ddfsdf')

Select * 
      ,Val1=dbo.udf_GetNumeric(substring(MyString,1,charindex('/',MyString)))
      ,Val2=dbo.udf_GetNumeric(substring(MyString,charindex('/',MyString),50))
 From @Table

返回

MyString                Val1    Val2
nfsdklj6 / 3 ddfsdf     6       3
nfsdklj45 / 100 ddfsdf  45      100

针对您的具体表格

Select Shuma 
      ,Val1=dbo.udf_GetNumeric(substring(Shuma,1,charindex('/',Shuma)))
      ,Val2=dbo.udf_GetNumeric(substring(Shuma,charindex('/',Shuma),50))
 From [dbo],[Deals] 
 Where Shuma not like LIKE '%1 / 1%'