How to check if a value is a number in SQLite

后端 未结 9 1963
-上瘾入骨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:23

    To test whether the column contains exclusively an integer with no other alphanumeric characters, use:

    NOT myColumn GLOB '*[^0-9]*' AND myColumn LIKE '_%'
    

    I.e., we test whether the column contains anything else than a digit and invert the result. Additionally we test whether it contains at least one character.

    Note that GLOB '*[0-9]*' will find digits nested between other characters as well. The function typeof() will return 'text' for a column typed as TEXT, even if the text represents a number. As @rayzinnz mentioned, the abs() function is not reliable as well.

    0 讨论(0)
  • 2020-12-10 15:24
    SELECT * 
    FROM mytable
    WHERE columnNumeric  GLOB '*[0-9]*'
    
    0 讨论(0)
  • 2020-12-10 15:29
    select * from mytable where abs(mycolumn) <> 0.0 or mycolumn = '0'
    

    http://sqlfiddle.com/#!5/f1081/2

    Based on this answer

    0 讨论(0)
提交回复
热议问题