Assuming there is a table called cities with columns name and country, why doesn’t this query work?
select
concat(upper(name, ', ', country)) as location
FROM
cities;
But the one below does?
select
upper(concat(name, ', ', country)) as location
FROM
cities;
I tried both approaches, but couldn’t figure out why the first did not work, but the second did.
Now if there are two or more operations, how do I decide on their ordering so the query works?
2
Answers
UPPER function only accepts a single string parameter. reference
In the first query, you are passing multiple params, so that causes error.
In your second query, you concat multiple params into a single string then pass it to UPPER, so it works.
Postgres’
UPPER()
function is defined to accept a single string input, not a CSV list of several strings as input. Apparently, your first query is working without uppercasing, but there is not any guarantee that Postgres should even accept that syntax.The second version is the correct one, where you concatenate first, then passed that concatenated string as a single input into the
UPPER()
function.