Short tips: SQLite – append text to existing fields

19/07/2013 | Add comment

We came to a situation where we needed to update a table with ~180 records with additional text. Like, every second record was supposed to contain the current value (string) plus append something else, so that if the original value was:

The quick brown fox jumps

we needed to append:

” over the lazy dog”

to it.

Luckily in SQL we have a conditional construction that serves as “IF” statement, for database management systems that would not support the IF THEN construction, and in SQLite its signature is like this:

So what we basically want to do is this: get every item whose id field modulus with 2 is 0 (2, 4, 6 …) and append ” over the lazy dog” at the end.

So the SQLite syntax to append text to every second record in the table ‘tablename’ would be:

Pay attention to the concatenation operator in SQLite. Here you can find more info about SQLite syntax.

Happy hacking!

Published in: development, tutorials