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
  • 429,241
  • 80
  • 483
  • 523
Ian Vink
  • 63,888
  • 100
  • 326
  • 544

4 Answers4

273

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
  • 429,241
  • 80
  • 483
  • 523
  • 31
    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
42

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

select  locationname || '<p>' from location;
shamittomar
  • 45,002
  • 12
  • 73
  • 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
  • 17,878
  • 8
  • 77
  • 67
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
  • 71
  • 8