Page 1 of 1

Removing leading\trailing zeroes in TM1

Posted: Wed Oct 12, 2016 2:03 pm
by jack_008
How can I remove leading\trailing zeroes ?

for eg: I have a order ID- 000023567 in a source file. So,In order to move only the 23567 portion of the order ID to the cube, what function or steps I can follow in TI Process to achieve the result? Also, the number of zeroes can vary for different order IDs.

Thanks in advance for the help.

Re: Removing leading\trailing zeroes in TM1

Posted: Wed Oct 12, 2016 2:16 pm
by qml
First of all - this is the wrong subforum, but I'm sure an Admin will kindly move the thread.

Stripping off zeroes can be done in a few ways, but I would generally convert the string to a number and then back to a string again:

Code: Select all

sShortOrderID = TRIM( STR( NUMBR( vLongOrderID ), 32, 0 ) );

Re: Removing leading\trailing zeroes in TM1

Posted: Wed Oct 12, 2016 2:19 pm
by declanr
Sadly TM1 doesn't have a Replace function - if it did you could just replace 0 with blank.
For leading zeroes in a purely numeric string you can just convert to a number and then back to string NumberToString ( StringToNumber ( vRecord ) )
But that obviously doesn't work for trailing zeroes and can't be used if you have any string in the record.

The all encompassing option is to loop each character and delete the zeroes.

Code: Select all

sReplace = '0';
sReplaceWith = '';
iCount = Long ( vRecord );
While ( iCount > 0 );
	If ( SubSt ( vRecord, iCount, 1 ) @= sReplace );
		vRecord = SubSt ( vRecord, 1, iCount - 1 ) | sRecplaceWith;
	EndIf;
	iCount = iCount - 1;
End;

Re: Removing leading\trailing zeroes in TM1

Posted: Thu Oct 13, 2016 4:17 pm
by declanr
Randomly popped into my head earlier that my previous code didn't factor in what comes after the changed character... something like below would be more useful should anyone see this in the future.

Code: Select all

sSuffix = '';
sReplace = '0';
sReplaceWith = '';
iCount = Long ( vRecord );
While ( iCount > 0 );
	If ( SubSt ( vRecord, iCount, 1 ) @= sReplace );
		vRecord = SubSt ( vRecord, 1, iCount - 1 ) | sReplaceWith | sSuffix;
	Else;
	         sSuffix = SubSt ( vRecord, iCount, 1 ) | sSuffix;
	EndIf;
	iCount = iCount - 1;
End;
Surprised no one pulled me up on this.