How to check if a value is a number in SQLite

后端 未结 9 1970
-上瘾入骨i
-上瘾入骨i 2020-12-10 14:29

I have a column that contains numbers and other string values (like \"?\", \"???\", etc.)

Is it possible to add an \"is number\" condition to the where clause in SQL

9条回答
  •  执笔经年
    2020-12-10 15:18

    For integer strings, test whether the roundtrip CAST matches the original string:

    SELECT * FROM mytable WHERE cast(cast(mycolumn AS INTEGER) AS TEXT) = mycolumn
    

    For consistently-formatted real strings (for example, currency):

    SELECT * FROM mytable WHERE printf("%.2f", cast(mycolumn AS REAL)) = mycolumn
    

    Input values:

    • Can't have leading zeroes
    • Must format negatives as -number rather than (number).

提交回复
热议问题