2017-12-13 6 views
1

が含まれている私はSQLを選択し、小数点日

現在のテーブル出力指定した文字列から数字のみ小数点他の整数値をしたい:

 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

SQLはこんなもののための最高のツールではありません。 – HoneyBadger

答えて

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 
関連する問題