T661, “Non-decimal integer literals”


BigQueryDb2 (LUW)DerbyH2MariaDBMySQLOracle DBPostgreSQLSQL ServerSQLite2005200720092011201320152017201920212023⚡ 3.8.11 - 3.50.0b⚡ 3.5.7 - 3.8.3a⚠ 2008R2 - 2022b✓ 16 - 17d⊘ 8.3 - 15⊘ 11gR1 - 23.9⚡ 5.0 - 9.3.0a⚡ 5.1 - 12.0.2a⚠ 2.2.220 - 2.3.232bc⚠ 1.4.192 - 2.1.214b⊘ 10.15.1.3 - 10.17.1.0⊘ 9.7 - 12.1.2⚠ 2.0b
  1. Wrong results in cast
  2. Only hexadecimal, not binary nor octal
  3. Only in literals, not in cast
  4. Also with underscores (_) between the digits

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).

BigQuery 2025-09-02Db2 (LUW) 12.1.2Derby 10.17.1.0H2 2.3.232MariaDB 12.0.2aaaMySQL 9.3.0aaaOracle DB 23.9PostgreSQL 17SQL Server 2022SQLite 3.50.0aaa0b1010100o520x2ACAST('0b101010' AS …)CAST('0o52' AS …)CAST('0x2A' AS …)
  1. Doesn’t fail, but returns the wrong result. E.g. just the “0“ before the first letter
Optional Features

Normative References

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.

About the Author

Photo of Markus Winand

Markus Winand provides insights into SQL and shows how different systems support it at modern-sql.com. Previously he made use-the-index-luke.com, which is still actively maintained. Markus can be hired as trainer, speaker and consultant via winand.at.

Buy the Book

Cover of “SQL Performance Explained”: Squirrel running on grass

The essence of SQL tuning in 200 pages

Buy now!
(paperback and/or PDF)

Paperback also available at Amazon.com.

Hire Markus

Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »

Footnotes

  1. ISO/IEC 9075-2:2023 §6.13 GR 8b

Connect with Markus Winand

Subscribe mailinglistsSubscribe the RSS feedMarkus Winand on LinkedInMarkus Winand on XINGMarkus Winand on TwitterMarkus Winand on Bluesky
Copyright 2015-2025 Markus Winand. All righs reserved.
Legal | Contact | NO WARRANTY | Trademarks | Privacy and GDPR