Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pad numbers with leading zeros in an Access query

I have a column of numbers between 0 - 6 digits long. For those less than 6 I need to pad out with zeros to ensure they are all 6 digits i.e 12563 = 012563 or 23 000023 etc etc. Can someone recommend a solution?

like image 573
A Hughes Avatar asked Apr 24 '14 14:04

A Hughes


People also ask

How can I pad a value with leading zeros?

To pad an integer with leading zeros to a specific length To display the integer as a decimal value, call its ToString(String) method, and pass the string "Dn" as the value of the format parameter, where n represents the minimum length of the string.

What is padding in number?

Padded numbers are frame numbers that have a specified number of digits, where 0s are used to fill the unused digits. For example, 5 digit padding myimage00002.ext.


2 Answers

Probably the easiest way to pad numbers with leading zeros would be to use the Format() function, as in

Format(fieldName, "000000")
like image 137
Gord Thompson Avatar answered Oct 19 '22 10:10

Gord Thompson


If you're searching on this (like for PIN numbers, where '12' would be represented as '000012' here's an example using Gord's correct answer;

SELECT CStr(Format(fieldName,"000000")) FROM table WHERE CStr(Format(fieldName,"000000"))="000012";
like image 2
David Barclay Avatar answered Oct 19 '22 11:10

David Barclay