Skip to Main Content
  • Questions
  • how to create column alias name from the value of another column

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Ravi.

Asked: May 18, 2017 - 2:22 pm UTC

Last updated: May 18, 2017 - 3:27 pm UTC

Version: 11

Viewed 10K+ times! This question is

You Asked

Hi Tom,

Table structure:
empno empname sal balance

i had select stmt which will accept date(18-May-2017) as parameter and
display the data for date given but column name for balance should be balance_0518

could you please help me in the query.

Thanks
Ravi

and Connor said...

If you want to dynamically set a column name, then the SQL itself must be dynamic. So you can use a ref cursor for that

SQL> create table t ( x date, bal number );

Table created.

SQL>
SQL> insert into t values ( date '2017-05-17', 10 );

1 row created.

SQL>
SQL> variable rc refcursor
SQL>
SQL> declare
  2    d date := date '2017-05-17';
  3  begin
  4    open :rc for 'select bal bal_'||to_char(d,'ddmm')||' from t where x = :1' using d;
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL>
SQL> print rc

  BAL_1705
----------
        10

1 row selected.



I'm not sure this is really a good idea though....

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library