I would like convert the column name to date. for example the column name is today, i want to convert it dynamically to today's date like MM/DD/YYYY .
-
What date do you want to use if the column name is not "today"? – Michael McGriff Dec 12 '14 at 17:44
-
possible duplicate of [How to rename a table column in Oracle 10g](http://stackoverflow.com/questions/13878383/how-to-rename-a-table-column-in-oracle-10g) – Michael McGriff Dec 12 '14 at 17:45
-
2My question is: why? – Ascalonian Dec 12 '14 at 18:24
-
@MichaelMcGriff I want to use the current date as the column name.. example: as of now the column name is "Today" i want it to be current date "12/12/2014". – user2560650 Dec 12 '14 at 19:56
-
I get that, I just thought you wanted to be more dynamic about it going forward. The link I put above should get you all you need to know then. – Michael McGriff Dec 12 '14 at 20:01
2 Answers
as of now the column name is "Today" i want it to be current date
You can't configure a column to change its name automagically. To reflect the current day or whatever else.
But, you can change the column name by using an alias when doing a query. In order to make the things the more transparent as possible, you might want to create a view. Here is an example:
-- Some table with a column named "TODAY"
CREATE TABLE T AS (SELECT LEVEL today FROM DUAL CONNECT BY LEVEL < 5);
-- Use PL/SQL to create a view on the given table
-- with a dynamic column name
DECLARE
today varchar(10) := TO_CHAR(SYSDATE,'DD/MM/YYYY');
query varchar(200) := 'CREATE OR REPLACE VIEW V'
|| ' AS SELECT today "' || today || '"'
|| ' FROM T';
BEGIN
execute immediate query;
END;
Then, to query the "table" with the right column name, you will simply need to query V
instead of T
:
SELECT * FROM V;
12/12/2014
1
2
3
4
If you recreate your view daily, say by calling the above PL/SQL code from a job, you will see each day a view with the current date as the column name. But, as the underlying table is left unchanged, you will still be able to query it using the canonical name today
. Which is important for example if you need to perform join on that table.
That being said, I'm not sure I will push toward such a solution. Use at your own risks!

- 50,096
- 7
- 103
- 125
If you want the column name heading to appear as something different than what the column name is defined in the table, you simply use the as "DisplayColumnName"
clause for that column:
select user_name, today as "12/12/2014" from some_table;
But you would need to programatically generate the SQL statement for that to work. What coding environment you are using would dictate how to dynamically create a select statement.

- 2,046
- 4
- 22
- 32