Mysql: How to get every rows that have more than a certain number of decimal after the dot

前端 未结 6 719
天命终不由人
天命终不由人 2020-12-19 17:12

I have a table that contains float values.

table

+   id   |  value  |
+--------|---------|
+   1    | 19.22   |
+   2    | 32.333  |
+   3    | 1.         


        
相关标签:
6条回答
  • 2020-12-19 17:32

    Try this:

    select value, LEN(value) - CHARINDEX('.', value) as DecimalCount from Table
    where LEN(value) - CHARINDEX('.', value) > 2
    

    NOTE: I'm not using mySQL, I have MSSQL. let me know if this works for you - just noticed you asked for a mySQL solution.

    UPDATE: Felt bad for answering for the wrong platform. A bit of research got me this for mySQL:

    select * from Table
    where LENGTH(SUBSTRING_INDEX(value, '.', -1)) > 2
    
    0 讨论(0)
  • 2020-12-19 17:42

    Cast the value column as a varchar and use string comparison.

    0 讨论(0)
  • 2020-12-19 17:42

    This worked for me:

    SELECT  *
    FROM table
    WHERE column <> ROUND (column,2)
    

    or:

    SELECT  *
    FROM table
    WHERE column <> CAST (column AS DECIMAL(36,2))
    
    0 讨论(0)
  • 2020-12-19 17:45

    SELECT * FROM table t WHERE floor(t.columnname)!=t.columnname

    0 讨论(0)
  • 2020-12-19 17:48

    This regex (MySQL 5.0+) worked for me, based on the data you provided:

    SELECT t.* 
      FROM YOUR_TABLE t 
     WHERE t.`value` REGEXP '[0-9]+.[0-9][0-9][0-9]+'
    

    Reference:

    • http://www.regular-expressions.info/
    0 讨论(0)
  • 2020-12-19 17:57

    This worked for me

    SELECT * 
    FROM `table` 
    WHERE LENGTH(SUBSTR(`value`,INSTR(`value`,"."))) >3
    

    Counting the decimal .01 is 3, .011 is 4

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