简体   繁体   English

在Oracle中声明和设置日期

[英]Declaring and setting dates in Oracle

I know in sql server I can declare dates by the following: 我知道在sql server中,我可以通过以下方式声明日期:

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? 但是我将如何在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. 在大多数工具中:SQL * plus,Toad for Oracle,SQL Developer等(以脚本运行),您可以使用DEFINE命令创建要在查询中使用的替换变量。

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 . 或者,您可以以标准的'yyyy-mm-dd'格式定义日期,而无需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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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