142

I am trying to execute a SQlite replace function, but use another field in the function.

select  locationname + '<p>' from location;

In this snip, the result is a list of 0s. I would have expected a string with the text from locationname and the '<p>' literals.

codaddict
  • 445,704
  • 82
  • 492
  • 529
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

4 Answers4

274

Try using || in place of +

select  locationname || '<p>' from location;

From SQLite documentation:

The || operator is "concatenate" - it joins together the two strings of its operands.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 32
    field1 || field2 returns null if one of the fields is null. One might want to do ifnull(field1,'')||ifnull(field2,''). That will give you a response if one or both fields are null. Then you get to figure out what you want to do if both were null. – Tom Cerul Oct 06 '11 at 14:55
  • 6
    @TomCerul or use `COALESCE(nullable_field, '') || COALESCE(another_nullable_field, '')` – zx8754 Sep 03 '14 at 13:51
  • The importance of TomCerul's comment is worthy rewriting or at least editing the correct answer. – Pepik Jan 24 '23 at 14:34
42

The || operator is the concatenation in SQLite. Use this code:

select  locationname || '<p>' from location;
shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • 11
    The double pipe is also the ANSI method of concatenating strings, supported on Oracle & PostgreSQL too... – OMG Ponies Aug 25 '10 at 18:04
37

For comparison,

SQLite                      ||  
Oracle                      CONCAT(string1, string2) or ||
MySQL                       CONCAT(string1, string2, string3...) or || if PIPES_AS_CONCAT enabled
Postgres                    CONCAT(string1, string2, string3...) or ||
Microsoft SQL Server 2012+  CONCAT(string1, string2, string3...) or + 
Microsoft Access            +  
Brian Burns
  • 20,575
  • 8
  • 83
  • 77
2

for Visual Studio 2010, using the Data Sources designer or wizard, you're in trouble using || operator. Create a view in the sqlite db and create your data source(s) from that.

See also this thread.

quickdraw
  • 101
  • 8