1

We have a system that requires users to input their ID into a field. User IDs are 6 digits long with leading 0's (when required).

For example: ID "789" is actually "000789"

Unfortunately, some users are not reading the instructions to include leading 0's, and we need a system to correct the input values when needed.

Our current solution is as follows:

%%[
VAR @input,@LENinput,@corrected
SET @input = QUERYPARAMETER("ID")
SET @LENinput = LENGTH(@input)

IF @LENinput == 1 THEN
    SET @corrected = Concat("00000",@input)
ENDIF
IF @LENinput == 2 THEN
    SET @corrected = Concat("0000",@input)
ENDIF
IF @LENinput == 3 THEN
    SET @corrected = Concat("000",@input)
ENDIF
IF @LENinput == 4 THEN
    SET @corrected = Concat("00",@input)
ENDIF
IF @LENinput == 5 THEN
    SET @corrected = Concat("0",@input)
ENDIF
IF @LENinput == 6 THEN
    SET @corrected = @input
ENDIF

/*Do things with @corrected input value*/
]%%

In Excel, we would achieve this with :

=RIGHT("000000"&[ID], 6)
6

We were able to solve with the following:

%%[
VAR @input,@inputmod,@corrected
SET @input = QUERYPARAMETER("ID")
SET @inputmod = concat("000000",@input)
SET @corrected = Substring(@inputmod, Add(Subtract(Length(@inputmod),6),1),6)
]%%

The Excel equivalent is:

=MID(A1,(LEN(A1)-6)+1, 6)
//Assuming Cell A1 is ="000000"&[ID]

Trick way of doing a RIGHT() function in AMPScript!

1
  • That really is a great solution! Thanks for sharing Mar 17 '17 at 13:16
1

Did you try:

var @input
set @input = FormatNumber(QueryParameter("ID"), "D6")
1
  • Your answer is phrased as a question. As such, it might have better been posted as a comment. If you believe this is the solution to the question, I suggest you rephrase to "I recommend trying:", doing, using, changing your code, etc.
    – crmprogdev
    Mar 18 '17 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.