Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
SELECT id, printf('%08d', id) AS padded FROM orders WHERE id <= 102 ORDER BY id;| id | padded |
|---|---|
| 101 | 00000101 |
| 102 | 00000102 |
%08d means an integer, at least 8 wide, padded with zeros.
PostgreSQL:
sql
SELECT id, LPAD(id::text, 8, '0') AS lpad, TO_CHAR(id, 'FM00000000') AS to_char,
LPAD('123456789', 8, '0') AS too_long
FROM orders WHERE id <= 102 ORDER BY id;| id | lpad | to_char | too_long |
|---|---|---|---|
| 101 | 00000101 | 00000101 | 12345678 |
| 102 | 00000102 | 00000102 | 12345678 |
LPAD pads a string on the left; if the input is longer than the target length, LPAD truncates it, which can silently cut ids. TO_CHAR prints # characters for numbers that do not fit. Choose the width with the largest expected value in mind.
Storing padded values as text breaks numeric sorting and range queries, so format at output time.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
SELECT id, printf('%08d', id) AS padded FROM orders WHERE id <= 102 ORDER BY id;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- SQLite: printf or format with %08d
- PostgreSQL: LPAD or TO_CHAR
- LPAD truncates values longer than the width
- Store numbers, format for display
Common mistakes
- Storing zero-padded ids as text and sorting them
- Choosing a width too small and truncating with LPAD
Follow-ups
- How would you format a price with two decimals?
- How would you right-pad a column for a fixed-width file?
Tips
- Note the truncation behaviour of LPAD