Wednesday, October 5, 2016

Adding Thousand separator in number field in Datastage

Datastage Realtime Scenario:

Requirement: When you have output field contains currency data and would need to put comma(,) as thousand separator.

Example : Input        -     Output
                  879769    -      879,769
                  123          -      123
                  3543543  -      3,543,543
                  23            -       23
Solution:

To fullfil the above requirement we can go for 2 design solutions.

   1. Create a parallel routine in cpp and call the function in Transformer stage.
     
        #include <iostream>
        #include <string>
        using namespace std;
        string AddCommas(char* a)
         {
           string numWithCommas = to_string(a);
            int insertPosition = numWithCommas.length() - 3;
            while (insertPosition > 0) {
               string b = numWithCommas.insert(insertPosition, ",");
      insertPosition-=3;}
            return b;
           }

   2. By using stage variable, Loop Condition in Transformer stage

                                                                Stage Variables

           1. Len(Inputcolumn)                                                                 =   Stglen
           2. If Stglen<=3 then 1 Else If Mod(Stglen,3)=0                       =   Stgnocommas
               Then  Len(Inputcolumn)/3-1 Else  Len(Inputcolumn)/3


                                                               Loop Condition

                                             Loop While : @ITERATION<=Stgnocommas

            1. If (@ITERATION=1 and Stglen<=3) then Inputcolumn Else      
                If @ITERATION=1 Then Left(Inputcolumn, Stglen-
                (3*@ITERATION)) :",":Right(Inputcolumn, 3* @ITERATION)           = LoopVar
                Else Left(LoopVar, Stglen-(3* @ITERATION)):",":
                Right(LoopVar, (@ITERATION-1)+(3* @ITERATION))          


                 
                                                                     Derivation

                                             Constaint : @ITERATION=Stgnocommas

             1.       LoopVar    =      Numwth_commas
























No comments:

Post a Comment