-2

In SQL Server we can declare a variable and set the date to that variable in query like this:

GETDATE() AS currentDate

How is this accomplished in Oracle?

ZCoder
  • 2,155
  • 5
  • 25
  • 62
  • 2
    Your title doesn't reflect the code in your question as Lukasz has explained below. Which is it... an alias or a variable that you need? Because both have been answered on Stackoverflow. [Variable](https://stackoverflow.com/questions/4925144/how-to-return-todays-date-to-a-variable-in-oracle)and [Alias](https://stackoverflow.com/questions/8451195/is-the-as-keyword-required-in-oracle-to-define-an-alias) – S3S Aug 15 '19 at 13:52
  • In SQLServer the GETDATE and GETUTCDATE function return the current server time with millisecond precision. To get the same in oracle you'll need to use the SYSTIMESTAMP function. Example: `SELECT SYSTIMESTAMP FROM DUAL` . If seconds precision is all you need you can use SYSDATE . DATETIME returned from sysdate is slightly easier to work with than a TIMESTAMP but primarily your choice should be driven by the precision required – Caius Jard Aug 15 '19 at 20:46

2 Answers2

2

GETDATE() AS currentDate is a column alias:

SELECT SYSDATE AS currentDate FROM dual

db<>fiddle demo


Variable declaration inside anonymous block:

DECLARE 
   currentDate DATE;
BEGIN
   currentDate := SYSDATE;
END;

db<>fiddle demo2

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
1

You can use INTO in a query in PL/SQL block and in your case, even you can assign it directly in PL/SQL block.

Declare
lv_date1 date;
lv_date2 date;
begin
select sysdate into lv_date1 from dual; --method 1
lv_date2 := sysdate; -- method 2
end;

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31