Is it possible to use aliases for a math operation in SQL Oracle? I am trying to perform the following query:
SELECT TITULO,AÑO,(TO_NUMBER(TO_CHAR(SYSDATE,'YYYY')) - AÑO)
"TRANSCURRIDOS" FROM PELICULA WHERE TO_NUMBER(TO_CHAR(SYSDATE,'YYYY')) >
25
What I would like to know is if the alias "Elapsed" could be used in the where, without repeating the mathematical statement.
Thank you very much.
What you want to do, directly, is not possible. Why? because in the query execution timeline the where occurs before the select, the engine filters first, extracts the data, creates the cursor and renders it. In this order of ideas, the alias does not yet exist when the engine is doing the where. One option (not recommended if the table is very large) is:
Or keep the query as you got it there.
I hope it helps you!
Currently you can't use aliases when inside the clause
WHERE
but you could implement a function that does the calculation and then call it wherever you want.Here is an example of creating a function:
To use it you just have to do:
I hope it helps.