Avatar of jsalestrom70
jsalestrom70
Flag 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
Microsoft Access

Avatar of undefined
Last Comment
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

8/22/2022 - Mon
peter57r

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
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HainKurt

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

DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Patrick Matthews

MX,

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

:)

Patrick
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

yep ... missed that
SOLUTION
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.