Build padded codes (INV-000042)

LPAD adds characters on the left to reach a fixed width.

SQL · Build padded codes (INV-000042)Intermediate
SELECT id,
       CONCAT('INV-', LPAD(id, 6, '0'))                         AS invoice_no,
       CONCAT('ORD-', DATE_FORMAT(created_at, '%Y%m'), '-', LPAD(id, 4, '0')) AS order_ref
FROM orders;
lpadconcatinvoice number