Problems / Pad Invoice Numbers / Editorial
LPAD
CAST(invoice_id AS VARCHAR)
'0'
'INV-' || year || '-' || padded
F.concat(F.lit(...), ..., F.lpad(...))
Zero-padding exists because string order and numeric order disagree: '7' > '10021' lexicographically. Fixed-width encoding makes the two orders coincide, which is why invoice numbers, log line IDs, and file names use it. The practical detail worth remembering: padding functions are string functions — the cast isn't decoration, and engines differ in whether they'd do it implicitly (and how they'd render the number if they did), so make it explicit.
'7' > '10021'
Solve Pad Invoice Numbers yourself →