简体   繁体   中英

Declaring and setting dates in Oracle

I know in sql server I can declare dates by the following:

declare @last_thursday date, @this_wednesday
set @last_thursday = '15-feb-2018'
set @this_wednesday = '21-feb-2018'

select name
from my_table
where upd_date between @last_thursday and @this_wednesday

But how would I be able to do that same pull in Oracle?

In most tools : SQL* plus, Toad for Oracle, SQL developer etc ( run as script ), you may use the DEFINE command to create substitution variables to be used in query.

ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY'; -- to set the session's default date format

DEFINE last_thursday =  "'15-feb-2018'"
DEFINE this_wednesday = "'21-feb-2018'"

select name
from my_table
where upd_date between &last_thursday and &this_wednesday;

Alternatively, you can define dates in the standard 'yyyy-mm-dd' format, which does not need ALTER SESSION . Note the usage of quotes in both cases.

DEFINE last_thursday  =  2018-02-15
DEFINE this_wednesday =  2018-02-21

select name
    from my_table
    where upd_date between DATE '&last_thursday' and DATE '&this_wednesday';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM