Mysql - concat string with another sql result - mysql

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

Related

Truncated incorrect double value in mysql [duplicate]

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

Getting the name with dashes using substring_index MySQL

I have a data that have this result:
I'm using the query
select substring_index(descriptn, ' ', -1) from table1
and I get my result along with the dash for example "JACQ-ARMIE"
I want to get only JACQ and ABBY. Can you give me hints on how to get the name? Does this also apply for substring_index?
Just use substring_index() again:
select substring_index(substring_index(descriptn, ' ', -1), '-', 1)
from table1;

How to select coordinate from mysql database

I can select column of type 'POINT' using
select AsText(column) from mytable;
It returns:
'POINT(1.2, 3.4)'
But can I select it as
'1.2, 3.4'
?
This can be done in backend (of course) but can mysql convert it for me?
There are special functions like ST_X, ST_Y, e.g.:
SELECT
CONCAT(ST_X(point_column), ', ', ST_Y(point_column))
FROM
points_table

Mysql Subquery as input second query

I want to calculate distance from mysql record, firstly I get all cordinates and saved as LineString object, but I have error. What is wrong with my sql?
WITH tmp AS
(SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') FROM track WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate)
SELECT ST_Length(ST_GeomFromText(tmp));
Firstly, MySQL doesn't support the WITH clause; secondly, you should define variables like 10.4 User-Defined Variables.
You can change you sql to this;)
SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') INTO #tmp
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;
SELECT ST_Length(ST_GeomFromText(#tmp));
Or just with one query:
SELECT ST_Length(ST_GeomFromText(GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ')))
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;

omit results starting with certain character

I am trying to retrieve a list of names from a table where the surname does not start with z or Z using mysql. I tried combining substring with instr to accomplish this. Attempt below:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'z'
OR SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'Z'
ORDER BY SName
My attempt is returning results with z as the first letter of the surname. Can anyone explain why? Or if there is a better way to achieve this
This can be much shortened with LIKE:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName NOT LIKE 'z%' AND FName NOT LIKE 'Z%';
IIRC LIKE is case-sensitive since MySQL v5.6.x
I wouldn't write it like
...WHERE LOWER(FName) NOT LIKE 'z%';
since applying functions on columns prevent MySQL from using the index on the column (if one exists).
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName REGEXP '^[^z]';