Jump to content

Select using Alias returns "0"


vitruvius

Recommended Posts

Hi,

 

I am trying to learn basics of the SQL and following the tutorials on w3schools. Yesterday I was reading SQL Aliases and "Trying it myself". The following code returns 10 rows, each of them being zero. Am I missing something?

SELECT FirstName + ' ' + LastName AS Name FROM Employees;

Thanks.

Link to comment
Share on other sites

16 hours ago, Ingolme said:

The + operator in SQL only does mathematical additions. To concatenate words, use the CONCAT () function. 

Thanks, good to know. But how come the following code works as expected:

SELECT City + ' ' + Country AS Address FROM Customers;

 

Link to comment
Share on other sites

Does that work? It depends on what kind of database you're using.

Here's a note from the tutorial page:

Quote

Note: To get the SQL statement above to work in MySQL use the following:


SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address
FROM Customers;

 

 

Link to comment
Share on other sites

Assuming you're only using MySQL, you can also look up the documentation.  Here's the list of operators, note that for + that it only indicates that it is the addition operator:

https://dev.mysql.com/doc/refman/5.7/en/non-typed-operators.html

Here's the list of string functions, this includes both concat and concat_ws.  The description for concat doesn't indicate that MySQL has another concatenation operator.

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...