Postgres query to check a string is a number

前端 未结 6 1345

Can anyone tell me the query to check whether a string is a number(double precision). It should return true if the string is number. else it should return false.

con

6条回答
  •  被撕碎了的回忆
    2020-12-01 18:18

    If you want to check with exponential , +/- . then the best expression is :

    ^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
    

    resulting in:

    select '12.41212e-5' ~ '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' ;
    

    as true.

    The expression is from: https://www.regular-expressions.info/floatingpoint.html

    You can check for other types of numbers, for example if you expect decimal, with a sign.

     select '-12.1254' ~ '^[-+]?[0-9]*\.?[0-9]+$';
    

提交回复
热议问题