SELECT CONCAT alternative in PostgreSQL

1.3k Views Asked by At

How can I write the following MySQL query in PostgreSQL syntax:

SELECT CONCAT(REPEAT("   ", (COUNT(parent.name) - 1)), cat.name) AS name

Thanks in advance!

2

There are 2 best solutions below

5
On BEST ANSWER

The error here is that PostgreSQL doesn't allow double quotes " for literal strings (as per the SQL standard). You'll need to use single quotes '.

You also need to cast (COUNT(parent.name) - 1) to an integer, either using ::int (specific to Postgre) or CAST(... AS int).
Note that this may raise an integer out of range error if (COUNT(parent.name) - 1) is superior to 2147483647.

SELECT CONCAT(REPEAT('   ', (COUNT(parent.name) - 1)::int), cat.name) AS name

Note that you can also use || for string concatenation :

SELECT REPEAT('   ', (COUNT(parent.name) - 1)::int) || cat.name AS name
0
On
SELECT REPEAT("   ", (COUNT(parent.name) - 1)) || cat.name AS name

SQLFIDDLE