Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL concat() and lower() weirdness

Tags:

mysql

Any idea why this works sensibly*:

mysql> select lower('AB100c');
+-----------------+
| lower('AB100c') |
+-----------------+
| ab100c          |
+-----------------+
1 row in set (0.00 sec)

But this doesn't?

mysql> select lower(concat('A', 'B', 100,'C'));
+----------------------------------+
| lower(concat('A', 'B', 100,'C')) |
+----------------------------------+
| AB100C                           |
+----------------------------------+
1 row in set (0.00 sec)

*sensibly = 'the way I think it should work.'

like image 368
shanusmagnus Avatar asked May 06 '11 04:05

shanusmagnus


People also ask

What is the difference between concat and Concat_ws in MySQL?

Both CONCAT() and CONCAT_WS() functions are used to concatenate two or more strings but the basic difference between them is that CONCAT_WS() function can do the concatenation along with a separator between strings, whereas in CONCAT() function there is no concept of the separator.

What can I use instead of concat?

Re: Alternatives to using Concat for SQL queriesTextJoin() is used to combine contents from different cells. You can specify a delimiter and you can ignore empty cells. If you want to use TextJoin, then all the elements need to be in cells.

Does concat work in MySQL?

CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.

Is concat faster than &?

The only real difference is the 255 strings limit of the CONCATENATE function and no such limitation when using the ampersand. Other than that, there is no difference between these two methods, nor is there any speed difference between the CONCATENATE and "&" formulas.


2 Answers

As stated on MySql String functions:


LOWER(str)

LOWER() is ineffective when applied to binary strings (BINARY, VARBINARY, BLOB).


CONCAT(str1,str2,...)

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast.


In your code you are passing 100 as a numeric so concat will return a binary string and lower is ineffective when applied to binary strings that's why it's not get converted. If you want to convert you can try this:

select lower(concat('A', 'B', '100','C'));
like image 122
Harry Joy Avatar answered Oct 27 '22 00:10

Harry Joy


lower is used to convert STRINGS to lowercase. But your value 100 is considered numeric. If you want to still achieve the result of lower case conversion, you should enclose the number in quotes like this:

select lower(concat('A', 'B', '100','C'));

I've tested this and it works fine.

like image 45
itsols Avatar answered Oct 26 '22 23:10

itsols