Is there a function for reversing string in SQLite? I'm unable to find anything in the documentation.
SQLite substr() returns the specified number of characters from a particular position of a given string. A string from which a substring is to be returned. An integer indicating a string position within the string X. An integer indicating a number of characters to be returned.
The SQLite substr function returns a substring from a string starting at a specified position with a predefined length.
Since SQLite strings do not normally contain NUL characters, the length(X) function will usually return the total number of characters in the string X. For a blob value X, length(X) returns the number of bytes in the blob.
SQLite sqlite_version function returns the version of the SQLite library.
There is no builtin function for that. You can add custom function, like in this example in Python:
import sqlite3
conn = sqlite3.connect("")
conn.create_function("strrev", 1, lambda s: s[::-1])
cur = conn.cursor()
cur.execute(r''' SELECT strrev('hello, world') ''')
print(cur.fetchone()[0]) #dlrow ,olleh
Using a common table expression it is possible to reverse a string in SQLite.
WITH reverse(i, c) AS (
values(-1, '')
UNION ALL SELECT i-1, substr('dlrow olleh', i, 1) AS r FROM reverse
WHERE r!=''
) SELECT group_concat(c, '') AS reversed FROM reverse;
Returns hello world
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With