Search code examples
sqloracleselectplsqlconcatenation

How to concatenate two columns that have alias PLSQL


I just started with PL/SQL and I am not very clear about the concept of concatenating so now I have the need to concatenate the result of two columns that have aliases and show the result of this in one.

The following are the queries I make with their respective aliases:

(SELECT SPRCMNT_TEXT
        FROM SPRCMNT
        WHERE SPRCMNT_PIDM = SHRDGMR_PIDM
        AND SPRCMNT_CMTT_CODE = '15'
        AND SPRCMNT_ORIG_CODE = 'FTDF') FOLIO_CONTROL,

(SELECT REGEXP_SUBSTR(SPRCMNT_TEXT_NAR, 'Profesionista="([^"]+)"', 1, 1, NULL, 1)
        FROM SPRCMNT
        WHERE SPRCMNT_PIDM = SHRDGMR_PIDM
        AND SPRCMNT_CMTT_CODE = '15'
        AND SPRCMNT_ORIG_CODE = 'FTDF') PROFESIONISTA,

What I need is to concatenate the alias of PROFESIONISTA+FOLIO_CONTROL and its result display it in a new column.


Solution

  • Both queries are the same excepted for the from clause, so I think that you want:

    SELECT 
        SPRCMNT_TEXT 
        || REGEXP_SUBSTR(SPRCMNT_TEXT_NAR, 'Profesionista="([^"]+)"', 1, 1, NULL, 1) res
    FROM SPRCMNT
    WHERE 
        SPRCMNT_PIDM = SHRDGMR_PIDM 
        AND SPRCMNT_CMTT_CODE = '15' 
        AND SPRCMNT_ORIG_CODE = 'FTDF'
    

    This gives you a result with a single column, called res, with the two strings concatenated (which is what operator || does in Oracle).

    You might want to add a separator:

        SPRCMNT_TEXT 
        || ' -- '
        || REGEXP_SUBSTR(SPRCMNT_TEXT_NAR, 'Profesionista="([^"]+)"', 1, 1, NULL, 1) res