简体   繁体   中英

How to Concatenate Two Extracted Columns?

I need to display the total time Days & hours someone has checked in.

The code below creates the variables in seperate columns with no identifiers apart from the column header. I can also create a column which displays the total hours.

select id, fname, lname, othertablevariable,
extract(day from diff) Days,
extract(hour from diff) Hours
from(
select id, fname, lname, othertablevariable,
(CAST(checkout as timestamp)-CAST(checkin as timestampe)) diff
from table t1, table t2
where t1.id=t2.id);

The output comes out as:

id fname lname days hours

However, I want the days column and hours column to be a single column.

Ideally it could display as 4 days 3 hours . Is this possible?

You can use inner query in order to achieve it. Check the answer below:

select id, ..., Days || Hours ConcatenatedColumn
from
(
    select id, fname, lname, othertablevariable,
    extract(day from diff) Days,
    extract(hour from diff) Hours
    from(
    select id, fname, lname, othertablevariable,
    (CAST(checkout as timestamp)-CAST(checkin as timestampe)) diff
    from table t1, table t2
    where t1.id=t2.id)
)

You can achieve it by using CONCAT() function:

select id, fname, lname, othertablevariable,
       CONCAT(extract(day from diff), ' days ', extract(hour from diff), ' hours') AS dayshours
from (
....

The result will be X days Y hours

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM