RosettaCodeData/Task/Substring/DuckDB/substring.duckdb

21 lines
806 B
Plaintext

# For brevity, we will use a table-valued function for defining the sample string(s):
create or replace function s() as table (select '一二三四五六七八九十' as s);
# starting from n characters in and of m length: s[n: n+m-1]
select s[1:2] from s();
# starting from n characters in, up to the end of the string: s[n:]
select s[9:] from s();
# whole string minus the last character: .[:-2]
select s[0:-2] from s();
# starting from a known character within the string and of m length, say 2:
select s[ix:ix+(2-1)] from (select s, position('五' in s) as ix from s());
# starting from a known substring within the string and of m length, say 2:
select s[ix:ix+(2-1)] from (select s, position('五六' in s) as ix from s());
# For clarity we'll use DuckDB's 'list' output mode:
.mode list