1

I have the below select statement in hive .It executes perfectely fine.

In Hive

select
COALESCE(product_name,CONCAT(CONCAT(CONCAT(TRIM(product_id),' - 
'),trim(plan_code)),' - UNKNOWN')) as product_name
from table name;

I am trying to use the same select statement in POSTGRESQL and it throw me error saying "

Query execution failed

Reason:

SQL Error [42883]: ERROR: function concat(text, unknown) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.

In postgresql:

select
COALESCE(product_name,CONCAT(CONCAT(CONCAT(TRIM(product_id),' - 
'),trim(plan_code)),' - UNKNOWN')) as product_name
from table name;

Could some one throw some light on this ?

4
  • This should work for text columns. What are the types of the columns? Commented May 22, 2019 at 18:10
  • concat_ws() could simplify this string expression. postgresql.org/docs/current/functions-string.html Commented May 22, 2019 at 18:13
  • @klin: Data types are text Commented May 22, 2019 at 18:20
  • concat() was introduced in Postgres 9.1, probably you use an older version. Upgrade. Commented May 22, 2019 at 18:27

2 Answers 2

1

Instead of concat try with ||:

SELECT COALESCE(product_name, 
        (TRIM(product_id) || ' - ' || TRIM(plan_code) || ' - UNKNOWN')
       ) AS product_name 
FROM tablename;

or simply a single CONCAT as:

SELECT COALESCE(product_name, 
         CONCAT(TRIM(product_id)::text, ' - ', TRIM(plan_code)::text, ' - UNKNOWN') 
       ) AS product_name
FROM tablename;
Sign up to request clarification or add additional context in comments.

3 Comments

column data types are text . Even i tried above query defining data types explicitly .But it gives me the same error.
@user8545255 SELECT COALESCE(product_name, (TRIM(product_id) || ' - ' || TRIM(plan_code) || ' - UNKNOWN') ) AS product_name FROM tablename; is this working
Just be aware that || will return null if any of the strings to be concatenated are null. The concat function does not do that.
0

You can also consider using format function:

SELECT coalesce(product_name, format('%s - %s - UNKNOWN', trim(product_id), trim(plan_code)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.