Link to home
Start Free TrialLog in
Avatar of jsalestrom70
jsalestrom70Flag for United States of America

asked on

Padding with zeros with a dash

I have a field in a query 12345-1234 that I need to pad to 012345-001234
Each side must have 6 digits. How can I do this in a microsoft access query?

Thanks

Jaymes
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

You either have to create a new virtual fiewld , or else you have to run an update query against the table to change the stored values..

newvalue: "0" & left (fieldname, 6) & "00" & right(fieldname, 4)
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use this function (alt+F11, copy paste)
Function myFormat(s As String)
  Dim n1 As String, n2 As String
  Dim ns() As String
  ns = Split(s, "-")
  n1 = "000000" & ns(0)
  n2 = "000000" & ns(1)
  myFormat = Right(n1, 6) & "-" & Right(n2, 6)
End Function

SELECT myFormat("1234-5678")
001234-005678

Open in new window

Select query:

SELECT Table1.FIELD0 AS FIELD0Before, Format(Replace([FIELD0],"-",""),"000000-000000") AS FIELD0After
FROM Table1
WHERE (((Nz([FIELD0],"")>"")=True));

Update Query - make a BACKUP first:

UPDATE Table1 SET Table1.FIELD0 = Format(Replace([FIELD0],"-",""),"000000-000000")
WHERE (((Nz([FIELD0],"")>"")=True));

mx
MX,

If you try that on the sample input 12345-1234, the result you get back is 000123-451234.

:)

Patrick
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial