Get integer part of number

前端 未结 3 1342
情深已故
情深已故 2021-01-07 18:21

So I have a table with numbers in decimals, say

id    value
2323   2.43
4954  63.98

And I would like to get

id    value
232         


        
相关标签:
3条回答
  • 2021-01-07 18:37

    Assuming you are OK with truncation of the decimal part you can do: SELECT Id, CAST(value AS INT) INTO IntegerTable FROM NumericTable

    0 讨论(0)
  • 2021-01-07 18:51

    FLOOR,CAST... do not return integer part with negative numbers, a solution is to define an internal procedure for the integer part:

    DELIMITER //
    DROP FUNCTION IF EXISTS INTEGER_PART//
    
    CREATE FUNCTION INTEGER_PART(n DOUBLE)
    
    RETURNS INTEGER
    
    
    DETERMINISTIC 
    
    BEGIN
    
    IF (n >= 0) THEN RETURN FLOOR(n);
                ELSE RETURN CEILING(n);      
    END IF;
    
    
    END
    //
    
    MariaDB [sidonieDE]> SELECT INTEGER_PART(3.7);
    +-------------------+
    | INTEGER_PART(3.7) |
    +-------------------+
    |                 3 |
    +-------------------+
    1 row in set (0.00 sec)
    
    MariaDB [sidonieDE]> SELECT INTEGER_PART(-3.7);
    +--------------------+
    | INTEGER_PART(-3.7) |
    +--------------------+
    |                 -3 |
    +--------------------+
    1 row in set (0.00 sec)
    

    after you can use the procedure in a query like that:

     SELECT INTEGER_PART(value) FROM table;
    

    if you do not want to define an internal procedure in the database you can put an IF in a query like that:

     select if(value < 0,CEILING(value),FLOOR(value)) from table ;
    
    0 讨论(0)
  • 2021-01-07 18:58
    SELECT FLOOR(value)
    

    http://msdn.microsoft.com/en-us/library/ms178531.aspx

    FLOOR returns the largest integer less than or equal to the specified numeric expression.

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