Quantcast
Channel: SCN : All Content - Data Services and Data Quality
Viewing all articles
Browse latest Browse all 4013

Custom function to count character occurrence Using SAP Data Services

$
0
0

Introduction

This article shows how to Create custom functions in Data Services and provides a solution for finding the character count within a string.

 

Scenario

Let’s Consider below source data.

String

AAABBBCCCDDDEEE

AAAAAAABBCCDDDE

AAAAAAAAAAAAABB

Our goal is to find out the number of occurrences of every character within a string. In this example, we will find count of occurrence of character "A","B","C","D" and "E". We will create a custom function to acheive the desired result.

 

Desired Output:

Count of A

Count of B

Count of C

Count of D

Count of E

3

3

3

3

3

7

2

2

3

1

13

2

0

0

0

 

Implementation:

.

Step 1 : Login to Data Service Designer.

            Click Custom Function(in Local Object Library) -> Right Click Custom Function and select "New".

1.jpg

Step 2 : Fill Properties

2.jpg

 

Enter the Function Name Function Description & Click Next.

Step 3 :  Create parameters

1)      $Input – Column Name in the source schema that holds the string

2)      $Value – This variable will store the character for which count is to be calculated

 

Creating Parameter:  $input.

3.jpg

 

Right Click Parameters & Click Insert to get the below Screen

4.jpg

Enter Name            : $input

Data Type               : Varchar ( Source Field Data Type)

Parameter Type   : Input

Length                     : 50 ( Maximum Length of our Source Field will be 50)

Similarly Create one more field of $Value of type varchar(1) and parameter Type as Input.

 

Step 4 :

 

Below Code is used to find the occurrences of given character in a String

 

Modified.jpg

 

Custom Function Code:

 

# To find the occurance of character in a String

 

Return nvl( length($Input),0)-nvl(length(replace_substr($Input,$Value,'')),0);

 

Step 5 : Creating Job to Execute the Custom Function

 

8.jpg

 

 

 

Step 6 :  Double Click the Query Transformation and create new output column as shown below.

 

9.jpg

 

10.jpg

Output Column Should be a Type Int. Because the Value Return by Custom Function Will be of type int.

Click Ok and next Click fu.jpg  in the Query Transformation.

11.jpg

You will get the below screen.

12.jpg

Click Next.

 

13.jpg

These are two input Parameter we have defined in our custom function.

$input is used to hold the column name from the input schema.

$Values is used to hold the character for which count is to be calculated.

Click the Drop Down Button 14.jpg

 

15.jpg

 

Double Click the Table Name Task_1.

16.jpg

Pass the Column Name as Input To the Custom Function.

 

Press ok. Assign ‘A’ as value to $Value. Because here our aim is to find the occurrences of ‘A’ in the Column

VAR_TYPE ( Column Name)

17.jpg

 

Similarly Create other output Columns in Query Transformation To find the count of B,C,D and E character in a String.

$input will be Same for all the Output Column.

$Value will be Changed as ‘B’, ‘C’ for corresponding Output Column as shown Below.

 

Output Column 1 : Count of A, $input = VAR_TYPE, $Value = ‘A’

Output Column 2 : Count of B, $input = VAR_TYPE, $Value = ‘B’

Output Column 3 : Count of C, $input = VAR_TYPE, $Value = ‘C’

Output Column 4 : Count of D, $input = VAR_TYPE, $Value = ‘D’

Output Column 5 : Count of E, $input = VAR_TYPE, $Value = ‘E’

18.jpg

Step 8 : Let’s Validate and Execute the Job.

After Successfull job execution, the target template table will hold values as below.   

 

Final Output:       

 

21.jpg


Viewing all articles
Browse latest Browse all 4013

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>