SQL ZEROFILL attribute
A column attribute, not a function: it left-pads SELECT output with zeros up to the declared display width, and implies UNSIGNED.
- Category
- Strings
- Returns
string- In this category
- 42 of 172
Syntax
region SMALLINT ZEROFILL
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Yes | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
Examples
MySQL
CREATE TABLE codes (region SMALLINT(5) ZEROFILL);
MySQL
SELECT LPAD(CAST(region AS CHAR), 5, '0') AS region_code FROM codes; -- 00042
Good to know
- Deprecated as of MySQL 8.0.17: new schemas should not use it, and the paired display-width attribute is deprecated too.
- ZEROFILL implies UNSIGNED: a signed column silently becomes unsigned the moment you add the keyword.
- It pads in result formatting, not in storage: WHERE, joins and comparisons see the raw number; only the client display gains zeros.
- The in-query replacement is LPAD(CAST(n AS CHAR), width, '0'), where the intent is explicit and survives a move to another engine's LPAD.
Questions about ZEROFILL
Is ZEROFILL deprecated?
Yes. MySQL 8.0.17 deprecated ZEROFILL and display widths. Existing tables still work, but new schemas should pad in queries instead.
Does ZEROFILL change stored values?
No. Storage is the plain unsigned integer; the zeros appear when the value is sent to the client, formatted to the declared display width.
How do I get the same effect portably?
LPAD(CAST(n AS CHAR), width, '0') on MySQL, LPAD(n::text, width, '0') on PostgreSQL, LPAD(TO_CHAR(n), width, '0') on Oracle, printf('%05d', n) on SQLite and FORMAT(n, '00000') on SQL Server. A bare CAST(n AS CHAR) means CHAR(1) on PostgreSQL and Oracle, so it does not port.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: MySQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.