Test for numeric value?

前端 未结 3 994
别那么骄傲
别那么骄傲 2021-01-20 17:44

The vendor data we load in our staging table is rather dirty. One column in particular captures number data but 40% of the time has garbage characters or random strings.

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-20 18:20

    A fairly reliable (but somewhat hackish) way is to compare the string to its upper- and lower-case self (numbers don't have different cases). As long as your data that is bringing in characters only includes Latin characters, you should be fine:

    SELECT input, CASE
        WHEN UPPER(input) = LOWER(input) THEN TO_NUMBER(input)
        ELSE 0
    END AS output
    FROM source
    

    Another option would be to use the TRANSLATE function:

    SELECT input,
        CASE 
            WHEN TRANSLATE(CAST(input as CHAR(10)), '~~~~~~~~~~~~~', '0123456789-. ') = '~~~~~~~~~~' THEN CAST(input AS DECIMAL(12, 2))
            ELSE 0
        END AS num
    FROM x
    

提交回复
热议问题