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?
5 Answers
Probably the easiest way to pad numbers with leading zeros would be to use the Format()
function, as in
Format(fieldName, "000000")
-
Seems to only work in
Select
queries (as I'm sure it's supposed to). Can't seem to get it to work in anUpdate
to format an entire column with leading zeros– JaskierFeb 25, 2019 at 16:57
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";
I had a similar issue. I couldn't change the field on the actual file because it was a split database and it had to be changed on the data source (Database_be). I went to the data source and made the change from Number to Short Text to all tables and that was it... Like magic!!
Try:
Update TABLE set DIGITS = string(6- len(DIGITS),"0")
DIGITS TABLE is the table where your numbers are stored.
DIGITS is the field that contains your numbers.
The above does NOT work.
Corrected version:
Update TABLE set DIGITS = string(6- len(DIGITS),"0")&DIGITS
The number '6' can be altered for whatever the total length of your field.