Home > database >  concat type int for 2 col in presto sql
concat type int for 2 col in presto sql

Time:01-30

I have 2 columns of type int and I want to make a concat to two columns in presto syntax. it is possible? for example:

id:

  1. 345
  2. 997

age:

  1. 23
  2. 55

new_col:

  1. 34523

  2. 99755

I was trying to use the array function but it is not working :/

thanks to everyone!

CodePudding user response:

As presto can not convert automatically

CONCAT(cast(id as varchar),cast(age as varchar))

CodePudding user response:

You concatenate strings.

You calculate with integers.

So, multiply one column by 100, and add the other. And the result is another integer, not a string.

Data Types matter. And integers are way faster than strings.

WITH
-- your input, one table, don't use in final query ...
id(id,idcol) AS (
            SELECT 1, 345
  UNION ALL SELECT 2, 997
)
,
-- your input, other table, don't use in final query ...
age(id,agecol) AS (
            SELECT 1, 23
  UNION ALL SELECT 2, 55
)
-- real query starts here ...
SELECT
  id.id
, idcol * 100   agecol AS new_col
FROM id JOIN age USING(id)
ORDER BY 1
-- out  id | new_col 
-- out ---- ---------
-- out   1 |   34523
-- out   2 |   99755
  • Related