Finding the number of leading zeros

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
pavan_test
Premium Member
Premium Member
Posts: 263
Joined: Fri Sep 23, 2005 6:49 am

Finding the number of leading zeros

Post by pavan_test »

Hi All,

I have a unique requirement.

I have data coming in as 000232,00116
I have to capture the number of leading zeros and along with the 1st occurance of the number after the leading zeros.

Eg: i have data coming in as 000232.

in my output i should be able to see 42 (4 is the starting occurance after after leading zero and 2 is the 1st number after leading zero)

Eg-2: in coming data is: 00116

output data: 31 (3 is the 1st occurance of a number after leading zeros and 1 is the 1st number after leading zero)

anyone please give me suggestions.

Thanks & Regards
Mark
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

In my experience nearly all "unique requirements" of this nature are best solved with a custom routine. However, what you need can be done with (for example) stage variable expressions.

To get the location of the first non-zero character, trim the leading zeroes and subtract the length of that from the length of the original string.

To get the second digit you require, simply take the leftmost character from the trimmed string.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Nice. Had pondered this briefly today and hadn't come up with anything.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ShaneMuir
Premium Member
Premium Member
Posts: 508
Joined: Tue Jun 15, 2004 5:00 am
Location: London

Post by ShaneMuir »

Could you also use something like:

Code: Select all

Index("1,2,3,4,5,6,7,8,9",InputCol.String,1)
to get the first required digit then use the field function with the newly acquired digit as your starting point to select 1 char?

Would this carry more processing overhead?

Addendum: Actually this won't work at all, you would have to do 9 index() statements (I think)
Last edited by ShaneMuir on Tue Mar 18, 2008 5:03 pm, edited 1 time in total.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

I think so. Len() is probably the most efficient function in DataStage, because the length of a (variable length) string is stored as that string's prefix. You find the length even before you find the string!
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

I'd use the LEN() function as well. You know how long the numeric column is, when you convert from number to string and use LEN() and LEFT() functions to parse your result.
Post Reply