String中的SQL select decimal包含Date

时间:2017-12-13 07:21:33

标签: sql substring sql-server-2016

我只想要来自给定字符串

的数字十进制整数值

当前表格输出:

        Column
        10.40  (10/14 after @ 10.3)  
        10.1%
        10%
        10/12/2017 10.45
        8.2  10/12
        15.20% 10/12/17
        11/12
        > 10.50
        < 50.10

预期产出:

        10.40
        10.1
        10
        10.45
        8.2
        15.20
        NULL
        NULL
        NULL

尝试过查询:

        declare @strAlphaNumeric VARCHAR(max) ='completed 11/12 at Dr Lemons office'

         select 
        stuff(stuff( @strAlphaNumeric+'x',patindex('%[0-9][^0-9.]%', @strAlphaNumeric+'x') + 1, len( @strAlphaNumeric), '' ), 1, patindex('%[0-9]%',  @strAlphaNumeric) - 1,      
        '')         

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

首先,正如HoneyBadger所说,SQL不是正确的工具。但是,替代方式

select case when TRY_CONVERT(money, LTRIM(SUBSTRING(Col1, PATINDEX('%[^0-9./]%', Col1), LEN(Col1)))) is null then
             cast(REPLACE(Col1,  LTRIM(SUBSTRING(Col1, PATINDEX('%[^0-9./]%', Col1), LEN(Col1)+1)), '') as varchar)
      else
             cast(TRY_CONVERT(money, LTRIM(SUBSTRING(Col1, PATINDEX('%[^0-9./]%', Col1), LEN(Col1)))) as varchar)  end [Col1] from <table>

结果:

Col1
10.40 
10.1
10
10.45
8.2  
15.20
BLANK
BLANK