Excerpt |
---|
Repeats a string a specified number of times. The string can be specified as a String literal, a function returning a String, or a column reference. |
- Since the
REPEAT
function matches based on fixed numeric values, changes to the length or structure of a data field can cause your recipe to fail to properly execute. - The
REPEAT
function requires an integer value for the number of characters to match.
String literal example:
D code |
---|
derive type:single value:REPEAT('ha',3) |
Output: The string hahaha
is written to the new column.
Column reference example:
D code |
---|
derive type:single value:REPEAT(MyString,4) |
Output: The values of the MyString
column value are written four times in a row to the new column.
D code |
---|
derive type:single value:REPEAT(column_string,rpt_count) |
Argument | Required? | Data Type | Description |
---|
column_string | Y | string | Name of the column or string literal to be applied to the function |
rpt_count | N | integer (positive) | Count of times to repeat the string |
column_string
Name of the column or String literal to be repeated.
- Missing string or column values generate missing string results.
- String constants must be quoted (
'Hello, World'
). - Multiple columns and wildcards are not supported.
Required? | Data Type | Example Value |
---|
Yes | String literal, function, or column reference | myColumn |
rpt_count
Count of times to repeat the string.
- If the value is not specified, the default is
1
. - Value must a non-negative integer.
- References to columns of integer data type are not supported.
Required? | Data Type | Example Value |
---|
No | Integer (non-negative) | 5 |
Example - REPEAT string function
Source:
myStr | repeat_count |
---|
ha | 0 |
ha | 1 |
ha | 1.5 |
ha | 2 |
ha | -2 |
Transform:
D code |
---|
derive type:single value:REPEAT(myStr,repeat_count) as:'repeat_string' |
Results:
myStr | repeat_count | repeat_string |
---|
ha | 0 | |
ha | 1 | ha |
ha | 1.5 | |
ha | 2 | haha |
ha | -2 | |
Example - padding a string
In this example, the prodId values are supposed to be 8 characters in length. Somewhere in the analytics pipeline, the leading 0's were stripped. The following steps add them back.
Source:
prodName | prodId |
---|
w01 | 1 |
w02 | 10000001 |
w03 | 345 |
w04 | 10402 |
Transform:
First, you must calculate how many leading 0's need to be added back for each line:
D code |
---|
derive type:single value: 8 - LEN(prodId) as:'lenPad' |
Build the pad string based on the above value:
D code |
---|
derive type:single value:REPEAT('0',lenPad) as:'padString' |
Combine the above string with the prodId value:
D code |
---|
merge col: string,repeat_count as: 'column1' |
Results:After you remove the intermediate columns and rename column1
to prodId
, you should have the following:
prodName | prodId |
---|
w01 | 00000001 |
w02 | 10000001 |
w03 | 00000345 |
w04 | 00010402 |