I am trying to build a string using MySQL 5.5.15 but it seems to not work for more than two args:
mysql> select concat(id, name) as me from locations; # this works
mysql> select concat(id, name, website) as me from locations; # doesn't work
Some of the examples have > 2 args but it just doesn't seem to work. Should it work?
try using CONCAT_WS()
SELECT CONCAT_WS('', id, name, website) AS me FROM locations
-- ^ this is an empty char separator,
-- you can define what ever you want
MySQL CONCAT_WS()
This may be a wild guess but I think the value of column website is NULL. CONCAT may act differntly with CONCAT_WS() because it doesn't convert NULL values into default string.
Here's a simple DEMO: http://www.sqlfiddle.com/#!2/c8d79/3
Related
I am trying a simple select statement with a variable. The statement works fine if I change the like concat_ws('%', #S, '%'); to a string. It seems that the select statement is not picking up the SET variable. Help would be much appreciated. I am using Mysql80 workbench.
SET #S = "product";
SELECT distinct idproducts FROM mgjtest.vorutaflamedsamheit
WHERE productname like concat_ws('%', #S, '%');
````````````````````````````````````````````````````````````````
Simply use CONCAT to ensure wildcards on either side of variable value. Otherwise, CONCAT_WS which uses first argument as separator returns double wildcards at the end of string which is equivalent to single wildcard and yields undesired results.
LIKE 'product%%'
LIKE 'product%'
However, CONCAT will return wildcards as you expect:
LIKE '%product%'
I tried to use "||" for concatenation:
The query I used:
"SELECT id, name, age, age||id FROM myTable;"
This is the output:
Can anyone tell me why the output is not 201 (in first column) and 162(in second column)?? Also it gives similar outputs when I use two attributes that are of varchar datatype and the above two attributes are of int datatype.
Can anyone tell me why the output is not 201
its because, in mysql, you need to enable PIPES_AS_CONCAT. in order to work with ||
If the PIPES_AS_CONCAT SQL mode is enabled, || signifies the
SQL-standard string concatenation operator (like CONCAT()).
You can set it using phpmyadmin->variables->searchFor SQL_MODE
Refer mysql doc
But i would suggest you to use
CONCAT(columnName1, columnName2, ...)
There is no such concatenation in mySQL. This is Oracle SQL dialect. You have to use the CONCAT function in mysql
SELECT id, name, age, CONCAT(age,id) FROM myTable
visit the Mysql Documentations :
on this link you will find a list of String Functions and Operators
but you not use|| to concatenate caratcters or strings but do this :
SELECT id, name, age, concat(age,id) FROM myTable; or
SELECT id, name, age, concat_ws(' ',age,id) FROM myTable; if you want to space the age with id like this for example 23 1. 23 for age and 1 for id.
I am trying to getting just the first two words on sql query, I am using the match: ^\w{2}- but with no success because nothing is coming to me, I need to get those values
BA, CE, DF, ES, GO, I don't know how can I do that, below some data example.
SC&Tipo=FM
SC&Tipo=Web
SC&Tipo=Comunitaria
RS&Tipo=Todas
RS&Tipo=AM
RS&Tipo=FM
RS&Tipo=Web
RS&Tipo=Comunitaria
BA-Salvador&Tipo=12horas
CE-Fortaleza&Tipo=12horas
CE-Interior&Tipo=12horas
DF-Brasilia&Tipo=12horas
ES-Interior&Tipo=12horas
ES-Vitoria&Tipo=12horas
GO-Goiania&Tipo=12horas
MG-ZonaDaMata/LestedeMinas&Tipo=12horas
MG-AltoParanaiba&Tipo=12horas
MG-BeloHorizonte&Tipo=12horas
MG-CentroOestedeMinas&Tipo=12horas
Query: SELECT * FROM tabel WHERE filter REGEXP '^\w{2}-'
EDIT SOLVED:
To solve the query should be:
SELECT SUBSTRING(column, 1, 2) AS column FROM table WHERE column REGEXP '^[[:alnum:]_]{2}-'
MySQL doesn't support the character class \w or \d. Instead of \w you have to use [[:alnum:]]. You can find all the supported character classes on the official MySQL documentation.
So you can use the following solution using REGEXP:
SELECT *
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-'
You can use the following to get the result with regular expression too, using REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR(filter, '^[[:alnum:]]{2}-')
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-';
Or another solution using HAVING to filter the result:
SELECT REGEXP_SUBSTR(filter, '^[[:alnum:]]{2}-') AS colResult
FROM table_name
HAVING colResult IS NOT NULL;
To get the value before MySQL 8.0 you can use the following with LEFT:
SELECT LEFT(filter, 3)
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-';
demo: https://www.db-fiddle.com/f/7mJEmCkEiYhCYK3PcEZTNE/0
Using SUBSTRING(<column>, 1, 2) should also work..
More or less like below
SELECT
<column>
, SUBSTRING(<column>, 1, 2)
FROM
<table>
WHERE
SUBSTRING(<column>, 1, 2) IN ('BA' [,<value>..])
Some things are BNF (Backus-Naur form) in the SQL code.
<..> means replace with what you need.
[, ..] means optional unlimited repeat the comma in there is part off SQL syntax
I'm trying to join two columns of the same table but, if there are null values (in my case they are in the second column), I want to take anyway the row that interests me and, rather than putting the null value, I would put ''. The columns that I want to join are Surname and Name. In other words, I tried to use :
SELECT CONCAT(CSurname, ' ', CName)
FROM Client;
In this way if I have a valid value for surname and a null value for name I obtain null. I use MySql, thanks.
If you want to avoid the problem with a leading space, then the easiest way is probably CONCAT_WS():
SELECT CONCAT_WS(' ', CSurname, CName)
FROM Client;
Unlike most other functions, CONCAT_WS() ignores NULL values (except for the separator), greatly simplifying this logic -- particularly when you are working with more than two columns.
Without it, the equivalent logic could be expressed as:
SELECT CONCAT(COALESCE(CONCAT(CSurname, ' '), ''), COALESCE(CName, ''))
Try the ifnull function
SELECT CONCAT(CSurname, ' ', IFNULL(CName,'')) FROM Client;
I don't have a local mysql installation to try it out but the IFNULL function should achieve what you need.
I am writing a PHP and MySQL application in which i have to concatenate multiple column values into one single column.I would have used the concat() function,but it does not handle null values,and the concat_ws(),which does not return the result in the output i want.
What i need can be achieved in the Oracle database like this:
Select 'The Surname Is'||last_name from employees;
My Issue is how can i achieve this same result with MySQL..without using the above named functions?
CONCAT with IFNULL:
SELECT
CONCAT('The Surname Is ', IFNULL(last_name, 'sadly not available'))
FROM `employees`
#Minesh: CONCAT_WS does not 'take care' of NULL values. To illustrate this...
CONCAT_WS("~",house.name,house.address,house.type)
In the above example, if house.address is NULL the returned result will not contain a neat double tilda (~~) as expected. It will be a tilda separated list with only 1 tilda. eg "fun House~mansion"
You can also use CONCAT_WS function which takes care of NULL values
SELECT
CONCAT_WS(' ','The Surname Is',lastname)
FROM `employees`
Use coalesce to concat an empty string
select concat(coalesce(null, ''));
A little trick: Use empty string like separator with CONCAT_WS (Some times you wan't insert white spaces)
CONCAT_WS('','The Surname Is:',lastname)
FROM `employees`