1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Coalesce SQL to return the result with parenthesis (bracket) for the 2nd argument

Discussão em 'Outras Linguagens' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to create a new column which can show the "Date Created" from the history table left joined with current table from the date column "TIMESTAMP" I use Oracle Interactive Reporting (IR) SQL querying tool to pull the data from the database DB2 .History table column is DB2.TABLEH.TIMESTAMP B and the Current table is DB2.TIMESTAMP A

    I created a simple coalesce query like below which runs fine

    COALESCE(Min ( B.TIMESTAMP), A.TIMESTAMP)


    But I would like to have the result to say from which argument was fulfilled by having a parentheses if the second argument returned the result. I tried with concatenate function but getting the error as below. Here is the full SQL

    SELECT A.ID,COALESCE(Min ( B.TIMESTAMP), '('CONCAT CAST(A.TIMESTAMP AS VARCHAR(10) ) CONCAT')') AS DATE_CREATED
    FROM DB2.TABLE A
    LEFT OUTER JOIN DB2.TABLEH B ON A.ID=B.ID AND A.COUNTRY= B.COUNTRY
    WHERE A.COUNTRY='XXXXXXX'
    GROUP BY A.ID, A.TIMESTAMP


    Please note I cannot have this in another column with a CASE WHEN statement but need to show the results with parentheses ONLY if the 2nd argument is fulfilled within the same result.

    Below is the error I get when I run above SQL


    "BIC00004. DAL010059. An error occurred while accessing the database: THE OPERANDS OF AN ARITHMETIC OR COMPARISON OPERATION ARE NOT COMPARABLE. SQLCODE=-401, SQLSTATE=42818, DRIVER=3.72.44 Extended error description is: The data types of the operands for the operation "{0}" are not compatible."

    I would like the result to show like this. If the result is from 2nd argument from the current table.it should be in a parenthesis as shown below.

    Id Date Created
    001 08-01-2004
    002 (05-02-2000)

    Output Result

    Continue reading...

Compartilhe esta Página