How to efficiently convert text to number in Oracle PL/SQL with non-default NLS_NUMERIC_CHARACTERS?

前端 未结 4 1198
眼角桃花
眼角桃花 2020-12-10 14:10

I\'m trying to find an efficient, generic way to convert from string to a number in PL/SQL, where the local setting for NLS_NUMERIC_CHARACTERS settings is inpredictable -- a

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 14:59

    The following should work:

    SELECT to_number(:x, 
                     translate(:x, '012345678-+', '999999999SS'), 
                     'nls_numeric_characters=''.,''') 
      FROM dual;
    

    It will build the correct second argument 999.999999 with the efficient translate so you don't have to know how many digits there are beforehand. It will work with all supported Oracle number format (up to 62 significant digits apparently in 10.2.0.3).

    Interestingly, if you have a really big string the simple to_number(:x) will work whereas this method will fail.

    Edit: support for negative numbers thanks to sOliver.

提交回复
热议问题