skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search