query-776749d250c5795cb057b328b716059a
TODO
Use at
- https://query.wikidata.org/sparql
SELECT ?n ?formatted
WHERE
{
BIND ("," AS ?sep)
VALUES ?n { 1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890 12345678901
123456789012 1234567890123 12345678901234 123456789012345 }
BIND (STR(?n) AS ?str)
BIND (STRLEN(?str) AS ?len)
BIND (?len - CEIL(?len/3)*3 + 3 AS ?length_first)
BIND (SUBSTR(?str, 1, ?length_first) AS ?group1)
BIND (SUBSTR(?str, ?length_first + 1, 3) AS ?group2)
BIND (SUBSTR(?str, ?length_first + 4, 3) AS ?group3)
BIND (SUBSTR(?str, ?length_first + 7, 3) AS ?group4)
BIND (SUBSTR(?str, ?length_first + 10, 3) AS ?group5)
BIND (COALESCE(
IF(?len > 12, CONCAT(?group1, ?sep, ?group2, ?sep, ?group3, ?sep, ?group4, ?sep, ?group5) , 1/0),
IF(?len > 9, CONCAT(?group1, ?sep, ?group2, ?sep, ?group3, ?sep, ?group4), 1/0),
IF(?len > 6, CONCAT(?group1, ?sep, ?group2, ?sep, ?group3), 1/0),
IF(?len > 3, CONCAT(?group1, ?sep, ?group2), 1/0),
?group1
) AS ?formatted)
}
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v11("?formatted"):::projected
v6("?group1")
v7("?group2")
v8("?group3")
v9("?group4")
v10("?group5")
v4("?len")
v5("?length_first")
v2("?n"):::projected
v1("?sep")
v3("?str")
bind0[/"','"/]
bind0 --as--o v1
bind1[/VALUES ?n/]
bind1-->v2
bind10(["1^^xsd:integer"])
bind10 --> bind1
bind11(["12^^xsd:integer"])
bind11 --> bind1
bind12(["123^^xsd:integer"])
bind12 --> bind1
bind13(["1234^^xsd:integer"])
bind13 --> bind1
bind14(["12345^^xsd:integer"])
bind14 --> bind1
bind15(["123456^^xsd:integer"])
bind15 --> bind1
bind16(["1234567^^xsd:integer"])
bind16 --> bind1
bind17(["12345678^^xsd:integer"])
bind17 --> bind1
bind18(["123456789^^xsd:integer"])
bind18 --> bind1
bind19(["1234567890^^xsd:integer"])
bind19 --> bind1
bind110(["12345678901^^xsd:integer"])
bind110 --> bind1
bind111(["123456789012^^xsd:integer"])
bind111 --> bind1
bind112(["1234567890123^^xsd:integer"])
bind112 --> bind1
bind113(["12345678901234^^xsd:integer"])
bind113 --> bind1
bind114(["123456789012345^^xsd:integer"])
bind114 --> bind1
bind2[/"str(?n)"/]
v2 --o bind2
bind2 --as--o v3
bind3[/"string-length(?str)"/]
v3 --o bind3
bind3 --as--o v4
bind4[/"?len - numeric-ceil(?len / '3^^xsd:integer') * '3^^xsd:integer' + '3^^xsd:integer'"/]
v4 --o bind4
bind4 --as--o v5
bind5[/"substring(?str,'1^^xsd:integer',?length_first)"/]
v3 --o bind5
v5 --o bind5
bind5 --as--o v6
bind6[/"substring(?str,?length_first + '1^^xsd:integer','3^^xsd:integer')"/]
v3 --o bind6
v5 --o bind6
bind6 --as--o v7
bind7[/"substring(?str,?length_first + '4^^xsd:integer','3^^xsd:integer')"/]
v3 --o bind7
v5 --o bind7
bind7 --as--o v8
bind8[/"substring(?str,?length_first + '7^^xsd:integer','3^^xsd:integer')"/]
v3 --o bind8
v5 --o bind8
bind8 --as--o v9
bind9[/"substring(?str,?length_first + '10^^xsd:integer','3^^xsd:integer')"/]
v3 --o bind9
v5 --o bind9
bind9 --as--o v10
bind10[/"if(?len > '12^^xsd:integer',concat(?group1,?sep,?group2,?sep,?group3,?sep,?group4,?sep,?group5),'1^^xsd:integer' / '0^^xsd:integer')if(?len > '9^^xsd:integer',concat(?group1,?sep,?group2,?sep,?group3,?sep,?group4),'1^^xsd:integer' / '0^^xsd:integer')if(?len > '6^^xsd:integer',concat(?group1,?sep,?group2,?sep,?group3),'1^^xsd:integer' / '0^^xsd:integer')if(?len > '3^^xsd:integer',concat(?group1,?sep,?group2),'1^^xsd:integer' / '0^^xsd:integer')?group1"/]
v4 --o bind10
v6 --o bind10
v1 --o bind10
v7 --o bind10
v8 --o bind10
v9 --o bind10
v10 --o bind10
bind10 --as--o v11