Adding leading zero's to a string

irade92

Registered User.
Local time
Today, 18:17
Joined
Dec 26, 2010
Messages
229
In Clipper there is a function REPLICATE...which means adding leading zeros to some string to get desirefull lenght...for example REPLI("0",10-LEN(string))...What is correspond function in Access...Please Help
 
I usually do
Code:
right("0000000000" & [fieldname], x)
or
Code:
left([fieldname] & "00000000000", x)
, x being string length.
 
Or

Code:
Format([FieldName],"0000000000")
 
I think what irade92 is after is a dynamic way of padding, so something like this:
Code:
String(10 - Len("String"), "0")
 
Didn't know about that one either - does it do the same as my reply?
 
I usually do
Code:
right("0000000000" & [fieldname], x)
or
Code:
left([fieldname] & "00000000000", x)
, x being string length.

Thanks James
I tried your code in Immediate Window and it doesnt work. I have decimal numbers like 55.68 ...or 345.74....and I want to get 0000055.68 or 0000345.74... Lenght should be 10.
 
and the right function doesn't do that? Try
Code:
 right("0000000000" & cstr([fieldname]),10)
If the data is formatted as a number, it'll remove the leading zeros, I think. It has to be a string to leave the zeros in there.
 
Didn't know about that one either - does it do the same as my reply?
The String() function is the equivalent of the Replication() function, so I was just expressing that point. From the help files, String() - Returns a Variant (String) containing a repeating character string of the length specified. From msdn, Replicate() - Returns a character string that contains a specified character expression repeated a specified number of times. Your function isn't quite the same.

From irade's recent posts, it seems he/she isn't after the Replication() function equivalent afterall. I would go for DCrake's Format() suggestion.
 
try

format([FieldName],"0000000.00")

Yes..I tried this eather and it works in Immediate Window but when I put it in a Query it changes to format([fieldName],"000.000.000") Where to correct not to change the format
 
It cannot change the Format() condition, it is probably changing the result when viewed in Datasheet.

Is this a Currency data type? Did you apply a format in the Format property of that field? Also, write down some examples of what some records are like BEFORE and AFTER.
 
It cannot change the Format() condition, it is probably changing the result when viewed in Datasheet.

Is this a Currency data type? Did you apply a format in the Format property of that field? Also, write down some examples of what some records are like BEFORE and AFTER.

The field in the table is Single with 2 decimals...i.e 56.78....In Query design When I use Format([field],"0000000.00") and press Enter it changes to Format([field],"000.000.000") and the result is only 000 in Datasheet view
 
Change the data type from single to double.
 
I think what irade92 is after is a dynamic way of padding, so something like this:
Code:
String(10 - Len("String"), "0")

Many thanks to you..
Your suggestion works...STRING(10-LEN(STRING),"0") & STRING I got exactly what I want...0000059.68
Thanks
 

Users who are viewing this thread

Back
Top Bottom