how to concatenate zeros before the col value

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

how to concatenate zeros before the col value

Post by shalini11 »

Hi All,

The requirement is that I have to select the max of column value from a table and the add 1 to it and insert the new record.

The column is varchar(5). So if the number after adding 1 is less than 5 digits then, zeros should be added before the number to make the field of 5 digits.

For ex:

The input field has values:
00000
00001
00002
00003

Now the max will return 00003.
After adding 1 it becomes 4 and gets inserted with value 4. But it should be inserted as 00004.
I tried using len function and padchar also but not able to do it.

Could you pls help

Thanks
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Post by shalini11 »

hey, I got the solution.

I used the below derivation:

If Len(AspCodeCountSVar) ='0' Then AspCodeCountSVar Else Str('0',5-Len(AspCodeCountSVar)):AspCodeCountSVar

where AspCodeCountSVar contains the value after adding 1.
ShaneMuir
Premium Member
Premium Member
Posts: 508
Joined: Tue Jun 15, 2004 5:00 am
Location: London

Post by ShaneMuir »

shalini11 wrote: If Len(AspCodeCountSVar) ='0' Then AspCodeCountSVar Else Str('0',5-Len(AspCodeCountSVar)):AspCodeCountSVar
You could have also used something like
Right('00000':AspCodeCountSVar,5)
OddJob
Participant
Posts: 163
Joined: Tue Feb 28, 2006 5:00 am
Location: Sheffield, UK

Post by OddJob »

ShaneMuir wrote: You could have also used something like
Right('00000':AspCodeCountSVar,5)
You could also try writing a parallel routine to do this - nice opportunity to learn C++!
asorrell
Posts: 1707
Joined: Fri Apr 04, 2003 2:00 pm
Location: Colleyville, Texas

Post by asorrell »

In the testing I've done, I use Shane's method (slap extra zeroes on the front and truncate to length) because it is signficantly faster than any other method I've found. It is basically two operations, pad and truncate with no logical tests to perform.
Andy Sorrell
Certified DataStage Consultant
IBM Analytics Champion 2009 - 2020
Post Reply