DDC-670: Mysql function CONCAT doesn't allow more than 2 parameters #823

Closed
opened 2026-01-22 12:51:43 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 6, 2010).

Jira issue originally created by user jklehr:

Trying to use CONCAT() function in a DQL query that has three (or more) parameters with mysql and getting a parser exception:

"Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','"

Mysql supports 2 or more parameters in the CONCAT function.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

Originally created by @doctrinebot on GitHub (Jul 6, 2010). Jira issue originally created by user jklehr: Trying to use CONCAT() function in a DQL query that has three (or more) parameters with mysql and getting a parser exception: "Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','" Mysql supports 2 or more parameters in the CONCAT function. http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
admin added the Bug label 2026-01-22 12:51:43 +01:00
admin closed this issue 2026-01-22 12:51:44 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2010):

Comment created by @beberlei:

Yes, but only MySQL.

We have to support the common denominator across all our supported vendors.

You can either extend the concat function and overwrite the existing one as detailed in the documentation:

http://www.doctrine-project.org/projects/orm/2.0/docs/cookbook/dql-user-defined-functions/en#dql-user-defined-functions

Or use the following tirck:

CONCAT(a, CONCAT(b, CONCAT(c, d)))
@doctrinebot commented on GitHub (Jul 6, 2010): Comment created by @beberlei: Yes, but only MySQL. We have to support the common denominator across all our supported vendors. You can either extend the concat function and overwrite the existing one as detailed in the documentation: http://www.doctrine-project.org/projects/orm/2.0/docs/cookbook/dql-user-defined-functions/en#dql-user-defined-functions Or use the following tirck: ``` CONCAT(a, CONCAT(b, CONCAT(c, d))) ```
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2010):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Jul 6, 2010): Issue was closed with resolution "Won't Fix"
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2010):

Comment created by jklehr:

All other DB engines support concatenation of multiple strings/fields they just do it differently. Many DB engines don't have a CONCAT function at all, so what is the parser doing to support them?

double pipe operator (standard SQL):

SELECT first*name || ' ' || last_name AS full*name

Supported by: Oracle, Postgres, Firebird, IBM DB2, Informix, SQLite

operator:

SELECT first*name </ins> ' ' + last_name AS full*name

Supported by: MSSQL

CONCAT function (only two parameters):

SELECT CONCAT(CONCAT(first*name, ' '), last_name) AS full*name

Supported by: Oracle, 4D, Mysql

CONCAT function (more than two parameters):

SELECT CONCAT(first*name, ' ', last_name) AS full*name

Supported by: Mysql

I assume the parser is converting "CONCAT()" in DQL to the proper underlying DB engine's alternative since CONCAT isn't supported by many DB engines natively. Couldn't the parser convert "CONCAT(first_name, ' ', last_name)" into the supported SQL for the DB engine in use? The only "trick" would be for 4D (using multiple CONCATs).

I know DQL isn't SQL, but what version of SQL did you decide to mimic for these functions? Will the parser choke on "||" thinking it's an OR clause (like mysql)?

@doctrinebot commented on GitHub (Jul 6, 2010): Comment created by jklehr: All other DB engines support concatenation of multiple strings/fields they just do it differently. Many DB engines don't have a CONCAT function at all, so what is the parser doing to support them? double pipe operator (standard SQL): ``` SELECT first*name || ' ' || last_name AS full*name ``` Supported by: Oracle, Postgres, Firebird, IBM DB2, Informix, SQLite <ins> operator: ``` SELECT first*name </ins> ' ' + last_name AS full*name ``` Supported by: MSSQL CONCAT function (only two parameters): ``` SELECT CONCAT(CONCAT(first*name, ' '), last_name) AS full*name ``` Supported by: Oracle, 4D, Mysql CONCAT function (more than two parameters): ``` SELECT CONCAT(first*name, ' ', last_name) AS full*name ``` Supported by: Mysql I assume the parser is converting "CONCAT()" in DQL to the proper underlying DB engine's alternative since CONCAT isn't supported by many DB engines natively. Couldn't the parser convert "CONCAT(first_name, ' ', last_name)" into the supported SQL for the DB engine in use? The only "trick" would be for 4D (using multiple CONCATs). I know DQL isn't SQL, but what version of SQL did you decide to mimic for these functions? Will the parser choke on "||" thinking it's an OR clause (like mysql)?
Author
Owner

@doctrinebot commented on GitHub (Jun 5, 2012):

Comment created by tridem:

Try combining multiple CONCAT with quotes escaped:
CONCAT(CONCAT(cp.lastName, ', '), cp.firstName) AS label

Havn't found a "cleaner" solution yet.

@doctrinebot commented on GitHub (Jun 5, 2012): Comment created by tridem: Try combining multiple CONCAT with quotes escaped: CONCAT(CONCAT(cp.lastName, \', \'), cp.firstName) AS label Havn't found a "cleaner" solution yet.
Author
Owner

@crayner commented on GitHub (Jul 14, 2017):

solution I use in Doctrine 2.4+:
$concat = new Query\Expr\Func('CONCAT', $name[$k]);
$concat .= ' as ' . $k;
$concat = str_replace(',', ',' ',', $concat);
$this->query->addSelect($concat);

so $name[$k] is an array of fields, as many as you wish. I then add some spacing between the fields with the str_replace. Hope this helps someone.

Craig

@crayner commented on GitHub (Jul 14, 2017): solution I use in Doctrine 2.4+: $concat = new Query\Expr\Func('CONCAT', $name[$k]); $concat .= ' as ' . $k; $concat = str_replace(',', ',\' \',', $concat); $this->query->addSelect($concat); so $name[$k] is an array of fields, as many as you wish. I then add some spacing between the fields with the str_replace. Hope this helps someone. Craig
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#823