0

I used the following code to define a couple substitution variables

Select '&&my_name' "my name", '&&my_birthday' "date born" from dual;

I want to use the date_born variable in subsequent calls in the select statement but keep getting the ORA-00904 error. Below is the code I'm trying:

Select '&&my_name' "my name", '&&my_birthday' "date born", TO_DATE("date born") from dual;

or

Select '&&my_name' "my name", '&&my_birthday' "date born", TO_DATE(my_birthday) from dual;

Any ideas? I must be defining or calling the variable or column incorrectly.

William Robertson
  • 15,273
  • 4
  • 38
  • 44
Josh909
  • 13
  • 1
  • 3
  • 1
    [Reference a Calculated Column](https://stackoverflow.com/questions/413764/sql-server-reference-a-calculated-column#413784) The simple answer is, you cannot use column defined at the same level. You need to use expression twice. `Select '&&my_name' "my name", '&&my_birthday' "date born", TO_DATE('&&my_birthday') from dual;` – Lukasz Szozda Mar 18 '18 at 09:24
  • How are the variables `my_name` and `my_birthday` defined in SQL*Plus? – Bob Jarvis - Слава Україні Mar 18 '18 at 12:42

1 Answers1

2

DUAL doesn't have a "date born" column.

Try this:

with demo (my_name, date_born) as
     ( select '&&my_name', '&&my_birthday' from dual )
select my_name, to_date(date_born)
from   demo;

or this

select my_name, to_date(date_born)
from   ( select '&&my_name' as my_name, '&&my_birthday' as date_born from dual );

I have used standard naming rather than double-quoting everything.

to_date(date_born) is a bit of a gamble on the date format and language happening to match your session defaults. I would prefer to specify a date format, for example by ensuring that '&&my_birthday' is formatted as YYYY-MM-DD and then

select '&&my_name' as my_name, date '&&my_birthday' as date_born from dual;

Edit: alternatively, as lad2025 pointed out, since you are using && you can use the same variable multiple times in the same statement.

William Robertson
  • 15,273
  • 4
  • 38
  • 44