3

For some reason, I cannot combine the firstname and lastname from a sql table.

Here is what I have tried:

SELECT firstname, lastname, firstname + ' ' + lastname as fullname from students;

For some reason it is showing in the result "0" for each row under fullname, however the firstname and lastname columns are populated as normal.

Please help. ***Please note, I do not want to use CONCAT() function

Screenshot here: table

Thanks in advance

Cid
  • 14,968
  • 4
  • 30
  • 45
Bill
  • 53
  • 1
  • 4
  • 1
    ANSI SQL has `||` for concatenation. – jarlh Apr 16 '19 at 13:57
  • Post the table schema. – gh9 Apr 16 '19 at 13:57
  • 1
    By the way, why not using concat ? – Cid Apr 16 '19 at 14:18
  • @AxelH they are using concat in the duplicate – Cid Apr 16 '19 at 14:19
  • 1
    *"they are using concat in the duplicate"* and they are also mentioning `PIPES_AS_CONCAT` SQL mode in MySQL which allows the ANSI SQL string concatenation (`||`) operator @Cid which jarth also mentioned – Raymond Nijland Apr 16 '19 at 14:20
  • Some answers need to be edited to add details on how to use `PIPES_AS_CONCAT`. There is links, but links may die. There is comment, but some don't read them – Cid Apr 16 '19 at 14:23
  • *"Some answers need to be edited to add details on how to use PIPES_AS_CONCAT"* Also some old "unsafe" answers or tutorials should also be removed from the internet @Cid .. You can't avoid these things.. – Raymond Nijland Apr 16 '19 at 14:25
  • 1
    @RaymondNijland good luck removing W3School :-) – Cid Apr 16 '19 at 14:26
  • 1
    *"good luck removing W3School"* Why whats wrong with it....?? @Cib i think if i turn around and ask what is correct about it you done faster :-) – Raymond Nijland Apr 16 '19 at 14:30
  • I found another duplicate question that had an answer showing use of `PIPES_AS_CONCAT`. – Bill Karwin Apr 16 '19 at 15:11

2 Answers2

6

You are trying to add two strings. That doesn't make sense. Although some (well, really one, given that SQL Server is based on Sybase) database overloads + to mean string concatenation, the proper way to do it is either:

concat(firstname, ' ', lastname) as fullname

or as I prefer because it handles NULL values cleanly:

concat_ws(' ', firstname, lastname) as fullname

or in a database that supports ISO/ANSI operators:

(firstname || ' ' || lastname ) as fullname

This doesn't work in MySQL, unless you have set the mode PIPES_AS_CONCAT (see here).

In MySQL, you will get 0, because + is treated as addition and the two strings are (silently) converted to numbers, based on leading digits. Because both start with non-digits, they are both converted to 0.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
3

Just a guess, but maybe your SQL version uses || for the concatenation operator:

SELECT
    firstname,
    lastname,
    firstname || ' ' || lastname AS fullname
FROM students;

The reason you are seeing zero could perhaps be due to that the database is casting the string columns to numbers, to make the + operator make sense.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360