The negative integer, e.g. -1 in SUBSTR(myField,-1,1),
tells Sqlite to start at the rightmost character in the string.
The positive integer, e.g. 1 in SUBSTR(myField,-1,1),
tells Sqlite to pick just one character.
The ||, in e.g. SUBSTR(myField,-1,1)||,
invokes the group_concat(X) function.
The 'reversed', in SUBSTR(myField,-8,1) 'reversed',
tells Sqlite to put the reversed strings in a new column labeled reversed.
The SELECT statement above would normally be put inside
a CREATE VIEW AS statement.
Example
CREATE VIEW AS
CREATE VIEW reversed AS
SELECT myField,
SUBSTR(myField,-1,1)||
SUBSTR(myField,-2,1)||
SUBSTR(myField,-3,1)||
SUBSTR(myField,-4,1)||
SUBSTR(myField,-5,1)||
SUBSTR(myField,-6,1)||
SUBSTR(myField,-7,1)||
SUBSTR(myField,-8,1) 'reversed'
FROM myTable;
Update
If the string is 1000 characters long, as hamstergene points out, you will have to use your scripting language to create a function that do the two following operations.
Query the field to get string length.
Create the SELECT statement programmatically based on the count.
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;
As others have already explained, SUBSTR with a negative first parameter returns inverted text. This could be used with LENGTH to not depend on knowing the actual length of the field. So:
SELECT SUBSTR(myfield , -LENGTH (myfield), LENGTH (myfield)) as inverted_myfield FROM [...]
This will return myfield but with inverted String (inverted_myfield).
I solved my problem storing word reverses which I obtained via this PHP script:
Hello MartyIX,
Yes, Sqlite3 has a function you can use to reverse strings.
Reverse Strings in Sqlite
Conditions
Example
Imagine you got a table named myTable like this one.
In this case the column myField contains strings of 8 characters in length.
Query
Result
Explanation
The negative integer, e.g. -1 in SUBSTR(myField,-1,1),
tells Sqlite to start at the rightmost character in the string.
The positive integer, e.g. 1 in SUBSTR(myField,-1,1),
tells Sqlite to pick just one character.
The
||
, in e.g. SUBSTR(myField,-1,1)||
,invokes the group_concat(X) function.
The 'reversed', in SUBSTR(myField,-8,1) 'reversed',
tells Sqlite to put the reversed strings in a new column labeled reversed.
The SELECT statement above would normally be put inside
a CREATE VIEW AS statement.
Example
CREATE VIEW AS
Update
If the string is 1000 characters long, as hamstergene points out, you will have to use your scripting language to create a function that do the two following operations.
Perhaps hamstergene could help you with that.
Using a common table expression it is possible to reverse a string in SQLite.
Returns
hello world
.There is no builtin function for that. You can add custom function, like in this example in Python:
As others have already explained, SUBSTR with a negative first parameter returns inverted text. This could be used with LENGTH to not depend on knowing the actual length of the field. So:
This will return myfield but with inverted String (inverted_myfield).