【问题标题】:Is there any alternative to use MYSQL's ADDDATE() in ORACLE?在 ORACLE 中使用 MYSQL 的 ADDDATE() 有什么替代方法吗?
【发布时间】:2023-02-10 04:35:04
【问题描述】:

我有这个查询需要为 oracle sql 而不是它最初来自的 mysql 执行,但是我有添加()我看不到任何其他选择的功能添加日期因为它需要的参数比我真正需要的多..

除此之外,如果我尝试执行它,它还会指示错误

选择 0 我联合......

部分,说下面ORA-00923: 在预期位置找不到 FROM 关键字
也许在 oracle 中不允许执行 select 0 union select 1 union ...
任何建议或帮助我很感激,谢谢

SELECT 
                    ADDDATE('1970-01-01', t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i) selected_date
                FROM
                    (
                        SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
                    ) t0,
                    (
                        SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
                    ) t1,
                    (
                        SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
                    ) t2,
                    (
                    SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
                    ) t3,
                    (
                    SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
                    ) t4

【问题讨论】:

  • 你需要从某些东西中选择。也许是双重的?
  • @jarlh 哦,对了,我还没有意识到进行双重选择会起作用...我会试一试,谢谢!
  • 请检查Datetime arithmetic部分。您可以使用 date_variable + n,其中 n 是天数或显式 date_var + interval 'n' days
  • 您只是想向 DATE 或 TIMESTAMP 添加天数吗?
  • @Pugzly 约会

标签: sql oracle oracle-sqldeveloper


【解决方案1】:

在 Oracle 中,您必须从单行表 dual 中选择才能选择一行。没有 from 子句就不能选择。

如果您想生成日期,您将编写一个标准的 SQL 递归 CTE。 (自 8.0 版以来,这也是 MySQL 现在的典型方法。)

以下是为 1970 年选择所有日期的示例:

with dates (dt) as
(
  select date '1970-01-01' from dual
  union all
  select dt + interval '1' day from dates where dt < date '1970-12-31'
)
select dt from dates;

【讨论】:

    【解决方案2】:

    这是另一种选择 1970 年日期列表的方法。如果您想要不同的年份,请调整开始和结束日期,如果您想要不同的时间段,如秒、分钟、小时……

    
     ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
    
    
    with dt (dt, interv) as (
    select date '1970-01-01', numtodsinterval(1,'DAY') from dual
    union all
    select dt.dt + interv, interv from dt
    where dt.dt + interv <= date '1970-12-31')
    select dt from dt;
    /
    
    

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 1970-01-01
      • 2015-04-09
      • 2021-08-20
      • 2017-03-02
      • 2010-09-08
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多