CAST DECIMAL to INT

后端 未结 10 2299
攒了一身酷
攒了一身酷 2020-12-13 01:49

I\'m trying to do this:

SELECT CAST(columnName AS INT), moreColumns, etc
FROM myTable
WHERE ...

I\'ve looked at the help FAQs here: http://

相关标签:
10条回答
  • 2020-12-13 01:55

    your can try this :

    SELECT columnName1, CAST(columnName2 AS  SIGNED ) FROM tableName 
    
    0 讨论(0)
  • 2020-12-13 01:57

    The CAST() function does not support the "official" data type "INT" in MySQL, it's not in the list of supported types. With MySQL, "SIGNED" (or "UNSIGNED") could be used instead:

    CAST(columnName AS SIGNED)
    

    However, this seems to be MySQL-specific (not standardized), so it may not work with other databases. At least this document (Second Informal Review Draft) ISO/IEC 9075:1992, Database does not list "SIGNED"/"UNSIGNED" in section 4.4 Numbers.

    But DECIMAL is both standardized and supported by MySQL, so the following should work for MySQL (tested) and other databases:

    CAST(columnName AS DECIMAL(0))
    

    According to the MySQL docs:

    If the scale is 0, DECIMAL values contain no decimal point or fractional part.

    0 讨论(0)
  • 2020-12-13 02:03

    From the article you linked to:

    The type can be one of the following values:

    BINARY[(N)]

    CHAR[(N)]

    DATE

    DATETIME

    DECIMAL[(M[,D])]

    SIGNED [INTEGER]

    TIME

    UNSIGNED [INTEGER]

    Try SIGNED instead of INT

    0 讨论(0)
  • 2020-12-13 02:09

    A more optimized way in mysql for this purpose*:

    SELECT columnName DIV 1 AS columnName, moreColumns, etc
    FROM myTable
    WHERE ...
    

    Using DIV 1 is a huge speed improvement over FLOOR, not to mention string based functions like FORMAT

    mysql> SELECT BENCHMARK(10000000,1234567 DIV 7) ;
    +-----------------------------------+
    | BENCHMARK(10000000,1234567 DIV 7) |
    +-----------------------------------+
    |                                 0 |
    +-----------------------------------+
    1 row in set (0.83 sec)
    
    mysql> SELECT BENCHMARK(10000000,1234567 / 7) ;
    +---------------------------------+
    | BENCHMARK(10000000,1234567 / 7) |
    +---------------------------------+
    |                               0 |
    +---------------------------------+
    1 row in set (7.26 sec)
    
    mysql> SELECT BENCHMARK(10000000,FLOOR(1234567 / 7)) ;
    +----------------------------------------+
    | BENCHMARK(10000000,FLOOR(1234567 / 7)) |
    +----------------------------------------+
    |                                      0 |
    +----------------------------------------+
    1 row in set (8.80 sec)
    

    (*) NOTE: As pointed by Grbts, be aware of the behaviour of DIV 1 when used with non unsigned/positive values.

    0 讨论(0)
  • 2020-12-13 02:13

    You could try the FLOOR function like this:

    SELECT FLOOR(columnName), moreColumns, etc 
    FROM myTable 
    WHERE ... 
    

    You could also try the FORMAT function, provided you know the decimal places can be omitted:

    SELECT FORMAT(columnName,0), moreColumns, etc 
    FROM myTable 
    WHERE ... 
    

    You could combine the two functions

    SELECT FORMAT(FLOOR(columnName),0), moreColumns, etc 
    FROM myTable 
    WHERE ... 
    
    0 讨论(0)
  • 2020-12-13 02:13

    use this

    mysql> SELECT TRUNCATE(223.69, 0);
            > 223
    

    Here's a link

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