REPEAT Function

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.

Basic Usage

String literal example:

derive type:single value:REPEAT('ha',3)

Output: The string hahaha is written to the new column.

Column reference example:

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.

Syntax

derive type:single value:REPEAT(column_string,rpt_count)

ArgumentRequired?Data TypeDescription
column_stringYstringName of the column or string literal to be applied to the function
rpt_countNinteger (positive)Count of times to repeat the string

For more information on syntax standards, see Language Documentation Syntax Notes.

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.

Usage Notes:

Required?Data TypeExample Value
YesString literal, function, or column referencemyColumn

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.

Usage Notes:

Required?Data TypeExample Value
NoInteger (non-negative)5

Examples

Example - REPEAT string function

Source:

myStrrepeat_count
ha0
ha1
ha1.5
ha2
ha-2

Transform:

derive type:single value:REPEAT(myStr,repeat_count) as:'repeat_string'

Results:

myStrrepeat_countrepeat_string
ha0
ha1ha
ha1.5
ha2haha
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:

prodNameprodId
w011
w0210000001
w03345
w0410402

Transform:

First, you must calculate how many leading 0's need to be added back for each line:

derive type:single value: 8 - LEN(prodId) as:'lenPad'

Build the pad string based on the above value:

derive type:single value:REPEAT('0',lenPad) as:'padString'

Combine the above string with the prodId value:

merge col: string,repeat_count as: 'column1'

Results:

After you remove the intermediate columns and rename column1 to prodId, you should have the following:

prodNameprodId
w0100000001
w0210000001
w0300000345
w0400010402

Was this page helpful? Let us know how we did:

Send feedback about...

Google Cloud Dataprep Documentation
Need help? Visit our support page.