【问题标题】:Declaring and setting dates in Oracle在 Oracle 中声明和设置日期
【发布时间】:2023-03-12 21:35:01
【问题描述】:

我知道在 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

但是我如何才能在 Oracle 中进行同样的拉动呢?

【问题讨论】:

  • 视情况而定。你会怎么称呼它?爪哇? SQL*Plus? Business Objects 报告?

标签: sql oracle


【解决方案1】:

在大多数工具中: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;

或者,您可以以标准'yyyy-mm-dd' 格式定义日期,不需要ALTER SESSION。注意这两种情况下引号的用法。

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';

【讨论】:

  • 或者你可以define last_thursday = 2018-02-15,然后where upd_date between date '&last_thursday'等等。(不需要改变会话。)
  • @WilliamRobertson :谢谢,已添加到答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多