cast
cast
The SQL standard allows implementations to accept binary, octal and hexadecimal integers in statements.
SELECT 0b101010
, 0o52
, 0x2A
FROM …
You need to prefix binary numbers with 0b
, octal numbers with 0o
and hexadecimal numbers with 0x
. There must be no space between the prefix and the actual number. All letters are case insensitive: those in the prefix and those in hexadecimal numbers.
The literal syntax of SQL is also used when casting a character string to numeric types.0 Consequently, the examples are also valid in expressions such as cast('0x2a' as integer)
.
Integer literals with the radix (bases) 2, 8 and 16 is defined in ISO/IEC 9075-2:2023 as the optional feature T661, “Non-decimal integer literals”. It first appeared in the 2023 edition.
You can’t catch up on 20 years of SQL evolution in one day. Subscribe the newsletter via E-Mail, Bluesky or RSS to gradually catch up and to keep modern-sql.com on your radar.
The essence of SQL tuning in 200 pages
Buy now!
(paperback and/or PDF)
Paperback also available at Amazon.com.
Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »
ISO/IEC 9075-2:2023 §6.13 GR 8b