Sub-string extraction · substr
---
config:
flowchart:
useMaxWidth: false
---
flowchart TB
classDef ok fill:#d4edda,stroke:#28a745,color:#000
classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
O["Sub-string extraction · substr<br/>🟢8"]:::ok
subgraph G_ds["Dataset"]
direction LR
v_ds_start_len["substr(DS, start, length)"]:::ok
v_ds_start["substr(DS, start)"]:::ok
v_ds_start_placeholder["substr(DS, start, _)"]:::ok
v_ds_placeholder_len["substr(DS, _, length)"]:::ok
v_ds_noargs["substr(DS)"]:::ok
end
subgraph G_comp["Component"]
direction LR
v_comp_start_len["substr(comp, start, length)"]:::ok
v_comp_params["substr(comp, comp, comp)"]:::ok
end
subgraph G_sc["Scalar"]
direction LR
v_sc_start_len["substr(sc, start, length)"]:::ok
end
O --- G_ds
O --- G_comp
O --- G_sc
style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
click v_ds_start_len href "#form-ds-start-len"
click v_ds_start href "#form-ds-start"
click v_ds_start_placeholder href "#form-ds-start-placeholder"
click v_ds_placeholder_len href "#form-ds-placeholder-len"
click v_ds_noargs href "#form-ds-noargs"
click v_comp_start_len href "#form-comp-start-len"
click v_comp_params href "#form-comp-params"
click v_sc_start_len href "#form-sc-start-len"
Types
Takes integer, string · Gives string
Comes from · integer, string — 27 operators in 6 blocks 🟢27
General purpose operators (2) 🟢2
String operators (9) 🟢9
Numeric operators (4) 🟢4
Time operators (7) 🟢7
Aggregate and Analytic operators (2) 🟢2
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 37 operators in 8 blocks 🟢37
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Time operators (3) 🟢3
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Goes to · string — 16 operators in 5 blocks 🟢16
General purpose operators (1) 🟢1
String operators (10) 🟢10
Comparison operators (1) 🟢1
Data validation operators (1) 🟢1
Definitions (VTL-DL) (3) 🟢3
Goes to · operators that take any scalar — 49 operators in 9 blocks 🟢49
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Definitions (VTL-DL) (1) 🟢1
Forms
substr(DS, start, length) 🟢 · string, integer → string
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(DS_1, 1, 5): the first five characters of every value of both measures.- Manual:
Sub-string extraction, Example 2, output verbatim: “The substring starts from the startth character of the input string and has a number of characters equal to the
lengthparameter.”- Expected:
Me_1ishello,abcdeandpqrst;Me_2ismediu,shortand"this "(with its trailing space).
DS_r:= substr ( DS_1 , 1 , 5 );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | hello world | medium size text |
| 1 | B | abcdefghilmno | short text |
| 2 | A | pqrstuvwxyz | this is a long description |
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | hello | mediu |
| 1 | B | abcde | short |
| 2 | A | pqrst | this |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(DS, start) 🟢 · string, integer → string
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(DS_1, 7): every value of both measures from the 7th character to its end.- Manual:
Sub-string extraction, Example 1, output verbatim: “If
lengthis omitted or overcomes the length of the input string, the substring ends at the end of the input string.”- Expected:
Me_1isworld,ghilmnoandvwxyz;Me_2is" size text"(with its leading space),textands a long description.
DS_r:= substr ( DS_1 , 7 );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | hello world | medium size text |
| 1 | B | abcdefghilmno | short text |
| 2 | A | pqrstuvwxyz | this is a long description |
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | world | size text |
| 1 | B | ghilmno | text |
| 2 | A | vwxyz | s a long description |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(DS, start, _) 🟢 · string, integer → string
Placeholder for length 🟢 pandas ✅ · DuckDB ✅
- Tests:
the placeholder
_forlength:substr(DS_1, 2, _)issubstr(DS_1, 2)with the underscore written out.- Manual:
In the invocation, a non-specified optional operand “is substituted by an underscore or, if it is in the end of the invocation, can be omitted” (Overview and conventions, syntax conventions); Sub-string extraction: “If
lengthis omitted or overcomes the length of the input string, the substring ends at the end of the input string.”- Expected:
each value loses its first character:
hellogiveselloandabgivesb. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’slengthandinstrexamples show.
DS_r := substr(DS_1, 2, _);
| Id_1 | Me_1 |
|---|---|
| 1 | hello |
| 2 | ab |
| 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | ello |
| 2 | b |
| 3 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(DS, _, length) 🟢 · string, integer → string
Placeholder for start 🟢 pandas ✅ · DuckDB ✅
- Tests:
the placeholder
_forstartinsubstr(DS_1, _, 3).- Manual:
Sub-string extraction: “If
startis omitted, the substring starts from the 1st position.” and “Iflengthis omitted or overcomes the length of the input string, the substring ends at the end of the input string.” Its valid syntaxes includesubstr ( DS_1, _ , 3 ).- Expected:
each value keeps its first three characters:
hello worldgiveshel, andabgivesab, as the length of 3 overcomes it. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’slengthandinstrexamples show.
DS_r := substr(DS_1, _, 3);
| Id_1 | Me_1 |
|---|---|
| 1 | hello world |
| 2 | ab |
| 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | hel |
| 2 | ab |
| 3 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(DS) 🟢 · string → string
Start and length omitted 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(DS_1)with neitherstartnorlength.- Manual:
Sub-string extraction: “If
startis omitted, the substring starts from the 1st position.” and “Iflengthis omitted or overcomes the length of the input string, the substring ends at the end of the input string.”- Expected:
the substring runs from the 1st position to the end, so every value comes out as it went in. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s
lengthandinstrexamples show.
DS_r := substr(DS_1);
| Id_1 | Me_1 |
|---|---|
| 1 | hello world |
| 2 | ab |
| 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | hello world |
| 2 | ab |
| 3 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(comp, start, length) 🟢 · string, integer → string
Manual example 3 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(Me_2, 1, 5)insidecalc, where the result replaces the measureMe_2.- Manual:
Sub-string extraction, Example 3, output verbatim: “The substring starts from the startth character of the input string and has a number of characters equal to the
lengthparameter.”- Expected:
Me_2ismediu,shortand"this "(with its trailing space);Me_1is unchanged.
DS_r:= DS_1 [ calc Me_2:= substr ( Me_2 , 1 , 5 ) ];
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | hello world | medium size text |
| 1 | B | abcdefghilmno | short text |
| 2 | A | pqrstuvwxyz | this is a long description |
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | hello world | mediu |
| 1 | B | abcdefghilmno | short |
| 2 | A | pqrstuvwxyz | this |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(comp, comp, comp) 🟢 · string, integer → string
Start and length from components 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(Me_1, Me_2, Me_3)insidecalc:startandlengthcome from components, a different pair for each data point.- Manual:
Sub-string extraction types
startascomponent<integer [value >= 1]> | integer [value >= 1]andlengthascomponent<integer [value >= 0]> | integer [value >= 0], and “Iflengthis omitted or overcomes the length of the input string, the substring ends at the end of the input string.”- Expected:
Me_4isworld,bcdandbc:abchas only two characters from the 2nd, and the length of 10 overcomes them. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’slengthandinstrexamples show.
DS_r := DS_1[calc Me_4 := substr(Me_1, Me_2, Me_3)];
| Id_1 | Me_1 | Me_2 | Me_3 |
|---|---|---|---|
| 1 | hello world | 7 | 5 |
| 2 | abcdef | 2 | 3 |
| 3 | abc | 2 | 10 |
| 4 | 1 | 2 |
| Id_1 | Me_1 | Me_2 | Me_3 | Me_4 |
|---|---|---|---|---|
| 1 | hello world | 7 | 5 | world |
| 2 | abcdef | 2 | 3 | bcd |
| 3 | abc | 2 | 10 | bc |
| 4 | 1 | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
substr(sc, start, length) 🟢 · string, integer → string
Substring inside the string 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(SC_1, SC_2, SC_3)withSC_1=abcdefghijklmnopqrstuvwxyz,SC_2= 5 andSC_3= 10: astartand alengththat fit in the string.- Manual:
Sub-string extraction: “The substring starts from the startth character of the input string and has a number of characters equal to the
lengthparameter.”, as insubstr ("abcdefghijklmnopqrstuvwxyz", 5 , 10 )gives"efghijklmn".- Expected:
SC_risefghijklmn, a String: the 10 characters from the 5th,e, on.
SC_r := substr(SC_1, SC_2, SC_3);
| SC_1 | SC_2 | SC_3 |
|---|---|---|
| abcdefghijklmnopqrstuvwxyz | 5 | 10 |
Name |
Type |
Value |
|---|---|---|
|
String |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗
Length beyond the end 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(SC_1, SC_2, SC_3)withSC_1=abcdefghijklmnopqrstuvwxyz,SC_2= 25 andSC_3= 10, wherelengthovercomes what is left of the string.- Manual:
Sub-string extraction: “If
lengthis omitted or overcomes the length of the input string, the substring ends at the end of the input string.”, andsubstr ("abcdefghijklmnopqrstuvwxyz", 25 , 10 )gives"yz".- Expected:
SC_risyz, a String: only two characters are left from the 25th.
SC_r := substr(SC_1, SC_2, SC_3);
| SC_1 | SC_2 | SC_3 |
|---|---|---|
| abcdefghijklmnopqrstuvwxyz | 25 | 10 |
Name |
Type |
Value |
|---|---|---|
|
String |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗
Start beyond the end 🟢 pandas ✅ · DuckDB ✅
- Tests:
substr(SC_1, SC_2, SC_3)withSC_1=abcdefghijklmnopqrstuvwxyz,SC_2= 30 andSC_3= 10, wherestartis past the end of the string.- Manual:
Sub-string extraction: “If
startis greater than the length of the input string, an empty string is extracted.”, andsubstr ("abcdefghijklmnopqrstuvwxyz", 30 , 10 )gives"".- Expected:
SC_ris the empty string. The map compares an empty value and a null as the same one, so this example cannot tell the two apart.
SC_r := substr(SC_1, SC_2, SC_3);
| SC_1 | SC_2 | SC_3 |
|---|---|---|
| abcdefghijklmnopqrstuvwxyz | 30 | 10 |
Name |
Type |
Value |
|---|---|---|
|
String |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗