Truncated incorrect double value in mysql [duplicate] - mysql

I am using MySQL and MySQL Workbench 5.2 CE. When I try to concatenate 2 columns, last_name and first_name, it doesn't work :
select first_name + last_name as "Name" from test.student

MySQL is different from most DBMSs' use of + or || for concatenation. It uses the CONCAT function:
SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student
There's also the CONCAT_WS (Concatenate With Separator) function, which is a special form of CONCAT():
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
That said, if you want to treat || as a string concatenation operator (same as CONCAT()) rather than as a synonym for OR in MySQL, you can set the PIPES_AS_CONCAT SQL mode.

Try:
select concat(first_name,last_name) as "Name" from test.student
or, better:
select concat(first_name," ",last_name) as "Name" from test.student

Use concat() function instead of + like this:
select concat(firstname, lastname) as "Name" from test.student

Apart from concat you can also use concat_ws (concatenate with separator):
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
This function has the added benefit of skipping null values.
See https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws

That's not the way to concat in MYSQL. Use the CONCAT function Have a look here: http://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat

Related

Mysql - concat string with another sql result

Since I am new to mysql .
select concat ((select to_char(count(*)) from users) ,' of users') from dual;
(or)
select concat ((select to_char(count(*)) from users) ,' of users') ;
I am expecting "100 of users" as result set output. in oracle, the above-mentioned statement will work , but not in mysql. i am getting result value as 1.
You can use the following, using CONCAT (or CONCAT_WS) and a CAST to char:
-- using CONCAT
SELECT CONCAT(CAST(COUNT(*) AS CHAR), ' of users') FROM users;
-- using CONCAT_WS
SELECT CONCAT_WS(' ', CAST(COUNT(*) AS CHAR), 'of users') FROM users;
Instead of TO_CHAR you need to use CAST(... AS CHAR(5)). Since MySQL 8.0 the CONCAT can concat the result of COUNT(*) and of users without a CAST or CONVERT (explicit conversion).
demo: https://www.db-fiddle.com/f/3mVqPxrDZHURyMbX6px4L/0
try below way it will works below is an example not directly yours
SELECT CONCAT(CAST(COUNT(*) AS CHAR(5)), ' of users') FROM Vendor;
http://sqlfiddle.com/#!9/4704eb/18

MySQL: How to refer to an item already mentioned in a SELECT query

On a self-learn website, I used a SQLlite interface that allowed me to refer to an item that I just wrote.
Ex.:
SELECT CONCAT(fName, ' ', lName) AS **Name**, CONCAT(**Name**, ' ', ID)
FROM Players;
I tried doing that on MySQL (in case if it matters, I'm using the Cloud9 platform) and it doesn't work. Should it work and I'm simply not doing it right? Or there is no such thing?
It should not work. SQL (in general) does not guarantee the order of evaluation of expressions in a SELECT. Hence, it doesn't allow re-use of aliases defined in the same level.
In your case, the simplest solution is to repeat the expression:
SELECT CONCAT(fName, ' ', lName) AS Name, CONCAT(fName, ' ', lName, ' ', ID)
FROM Players;
Not elegant, but it gets the job done.
In MySQL, this is better than a subquery, because MySQL materializes subqueries, adding additional overhead.
You could try this:
SELECT CONCAT(A.Name, ' ', A.ID) FROM
(
SELECT ID, CONCAT(fName, ' ', lName) AS Name FROM PLAYERS A
) A
However, from a performance perspective, you are better off with the original query I think even if its longer.

I want to put 'given' and 'surname' column together into 'fullname' in SQL, how would i go about in doing so. tnx

I currently have them separated but would prefer to have both together in one column.
SELECT CONCAT(given, ' ', surname) AS fullname FROM tablename
or for null safe,
SELECT CONCAT_WS(' ', given, surname) AS fullname FROM tablename
you can concatenate them using:
select CONCAT(given,' ',surname) as fullname;

How to user single alias name for 2 columns at a time

i want to use single alias name in mysql for two columns at a time
first_name, lname as username
In username, i want values of both first_name & lname
thanx in advance
There is a function in php as CONCAT()
In your case you can use as,
CONCAT(first_name, ' ', lname) as username
Try to Concat fields
SELECT CONCAT(first_name, ' ', lname) as username FROM table
You can use CONCAT() function
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
CONCAT(first_name, ' ', lname) as username

String concatenation in MySQL

I am using MySQL and MySQL Workbench 5.2 CE. When I try to concatenate 2 columns, last_name and first_name, it doesn't work :
select first_name + last_name as "Name" from test.student
MySQL is different from most DBMSs' use of + or || for concatenation. It uses the CONCAT function:
SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student
There's also the CONCAT_WS (Concatenate With Separator) function, which is a special form of CONCAT():
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
That said, if you want to treat || as a string concatenation operator (same as CONCAT()) rather than as a synonym for OR in MySQL, you can set the PIPES_AS_CONCAT SQL mode.
Try:
select concat(first_name,last_name) as "Name" from test.student
or, better:
select concat(first_name," ",last_name) as "Name" from test.student
Use concat() function instead of + like this:
select concat(firstname, lastname) as "Name" from test.student
Apart from concat you can also use concat_ws (concatenate with separator):
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
This function has the added benefit of skipping null values.
See https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws
That's not the way to concat in MYSQL. Use the CONCAT function Have a look here: http://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat