Numeric Literals
Numeric literals represent integers, fixed-point numbers, or floating-point numbers.
Integral Number Syntax
[+|-]digit+[L|S|Y]digit is any decimal digit (0-9).
The data type of the integral number is determined by the optional postfix:
L(case-insensitive) indicatesBIGINTorLONG, an 8-byte signed integer.S(case-insensitive) indicatesSMALLINTorSHORT, a 2-byte signed integer.Y(case-insensitive) indicatesTINYINTorBYTE, a 1-byte signed integer.- If there is no postfix, the number is interpreted as an
INTorINTEGER, a 4-byte signed integer.
Integral Number Examples
SELECT -2147483600 AS col;
-- +-----------+
-- | col|
-- +-----------+
-- |-2147483600|
-- +-----------+
SELECT 9223372036854775807L AS col;
-- +-------------------+
-- | col|
-- +-------------------+
-- |9223372036854775807|
-- +-------------------+
SELECT -64Y AS col;
-- +----+
-- | col|
-- +----+
-- |-64 |
-- +----+
SELECT 512S AS col;
-- +----+
-- |col |
-- +----+
-- |512 |
-- +----+Fractional Number Syntax
A decimal literals can be written in the following forms.
decimaldecimal[exponent]BDwhole[exponent]BDThe BD postfix is required if the number is a whole number or has an exponent. Otherwise, the number would be interpreted as an integer or a double.
A double literal can be written in the following forms. The D postfix is optional when there is an exponent.
(decimal|whole)D(decimal|whole)exponent[D]A float literal can be written in the following forms. The F postfix is required in all forms.
(decimal|whole)F(decimal|whole)exponentFdecimal is a number that contains a decimal point.
[+|-]digit+.[+|-]digit+.digit+[+|-].digit+whole is a whole number without a decimal point.
[+|-]digit+exponent is the exponent in scientific notation.
(E|e)[+|-]digit+digit is any decimal digit (0-9).
In summary, fractional numbers are represented by decimal numbers or whole numbers, with optional exponent the following postfixes to indicate the data type:
D(case-insensitive) indicatesDOUBLE, an 8-byte double-precision floating point number.F(case-insensitive) indicatesFLOAT, a 4-byte single-precision floating point number.BD(case-insensitive) indicatesDECIMAL, with the total number of digits as precision and the number of digits to right of decimal point as scale.
Fractional Number Examples
SELECT 76.543 AS col, TYPEOF(76.543) AS type;
-- +-------+------------+
-- | col| type|
-- +-------+------------+
-- | 76.543|decimal(5,3)|
-- +-------+------------+
SELECT 8.21E1 AS col, TYPEOF(8.21E1) AS type;
-- +------+------+
-- | col| type|
-- +------+------+
-- | 82.1 |double|
-- +------+------+
SELECT -0.4321 AS col;
-- +--------+
-- | col|
-- +--------+
-- |-0.4321 |
-- +--------+
SELECT 250.BD AS col;
-- +-----+
-- | col |
-- +-----+
-- |250 |
-- +-----+
SELECT 6.9D AS col;
-- +-----+
-- | col |
-- +-----+
-- | 6.9 |
-- +-----+
SELECT -18BD AS col;
-- +----+
-- |col |
-- +----+
-- |-18 |
-- +----+
SELECT .789E3F AS col;
-- +------+
-- | col |
-- +------+
-- |789.0 |
-- +------+