【问题标题】:If there is empty date column then input current date and time如果日期列为空,则输入当前日期和时间
【发布时间】:2021-10-21 18:59:18
【问题描述】:

如果日期列为空,我想使用 Oracle SQL 输入当前日期和时间。

给定表格,

Login_Time Logout_Time
2021-08-20 09:57:00 2021-08-20 10:30:00
2021-08-20 10:00:00
2021-08-20 08:00:00

在 Logout Time 列是 null 然后我必须输入当前时间和日期。

所以我写了下面的查询,

SELECT "login_time", 
       CASE WHEN "logout_time" IS NULL THEN sysdate ELSE "logout_time" END AS "logout_time"
  FROM abc

我无法使用上述查询输入当前时间。谁能帮我解决这个问题

【问题讨论】:

  • 请解释一下,“我无法输入当前时间”是什么意思? sysdate 返回 date 数据类型,其中包含年、月、日、小时、分钟和秒。总是
  • 除了去掉包含列名的引号外,当前情况没有问题。此外,别名("logout_time")不需要引号。使用NVL2(logout_time,logout_time,sysdate) AS logout_time 可能是另一种快捷方式。

标签: sql oracle oracle11g


【解决方案1】:

我猜这两列都是date,而您正在使用sysdate 和默认NLS_DATE_FORMAT(这样您就不会在运行查询之前对其进行更改)

让我们用一个演示来试试吧

SQL> create table test_date ( logon_time date , logout_time date ) ;

Table created.

SQL> insert into test_date values ( sysdate-1/4 , sysdate ) ;

1 row created.

SQL> insert into test_date values ( sysdate-1/4 , sysdate ) ;

1 row created.

SQL> insert into test_date values ( sysdate-1/4 , sysdate ) ;

1 row created.

SQL> commit ;

Commit complete.

SQL> select * from test_date ;

LOGON_TIM LOGOUT_TI
--------- ---------
20-AUG-21 20-AUG-21
20-AUG-21 20-AUG-21
20-AUG-21 20-AUG-21

Oracle 将日期列存储为数据包,但您可以以任何您想要的格式显示这些值,只要它是有效的日期格式。默认情况下,数据库会按照默认NLS_DATE_FORMAT显示,您可以在视图NLS_DATABASE_PARAMETERS中找到。

SQL> select parameter,value from nls_database_parameters where parameter='NLS_DATE_FORMAT' ;

PARAMETER
--------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------
NLS_DATE_FORMAT
DD-MON-RR

但是我们可以通过几种方式来修改它。其中之一是更改会话级别

SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss' ;

Session altered.

SQL> select * from test_date ;

LOGON_TIME          LOGOUT_TIME
------------------- -------------------
20.08.2021 03:54:03 20.08.2021 09:54:03
20.08.2021 03:54:10 20.08.2021 09:54:10
20.08.2021 03:54:14 20.08.2021 09:54:14

SQL> insert into test_date values ( sysdate-1/8 , null ) ;

1 row created.

SQL> select * from test_date ;

LOGON_TIME          LOGOUT_TIME
------------------- -------------------
20.08.2021 03:54:03 20.08.2021 09:54:03
20.08.2021 03:54:10 20.08.2021 09:54:10
20.08.2021 03:54:14 20.08.2021 09:54:14
20.08.2021 06:55:15

SQL> select logon_time , case when logout_time is null then sysdate else logout_time end as logout_time from test_date ;

LOGON_TIME          LOGOUT_TIME
------------------- -------------------
20.08.2021 03:54:03 20.08.2021 09:54:03
20.08.2021 03:54:10 20.08.2021 09:54:10
20.08.2021 03:54:14 20.08.2021 09:54:14
20.08.2021 06:55:15 20.08.2021 09:55:46

【讨论】:

  • @unanoymous,这个答案能解决你的问题吗?如果是这样,请接受答案。真的很感激!
【解决方案2】:

这是我对问题的理解。

你说你不是

能够使用上述查询输入当前时间 (select "login_time", case when ...)

嗯,你当然不是。如果“输入”意味着您应该“更新”表格,那么您应该使用UPDATE,而不是SELECT

当前表格内容:

SQL> select * from test;

LOGIN_TIME          LOGOUT_TIME
------------------- -------------------
2021-08-20 09:57:00 2021-08-20 10:30:00
2021-08-20 10:00:00
2021-08-20 08:00:00

更新 NULL 值(仅针对示例表中的 2 行):

SQL> update test set logout_time = sysdate
  2  where logout_time is null;

2 rows updated.

结果:

SQL> select * from test;

LOGIN_TIME          LOGOUT_TIME
------------------- -------------------
2021-08-20 09:57:00 2021-08-20 10:30:00
2021-08-20 10:00:00 2021-08-20 18:55:55
2021-08-20 08:00:00 2021-08-20 18:55:55

SQL>

对于将来的插入,设置列的默认值,以便 Oracle 在 logout_time 不存在时插入它:

SQL> alter table test modify logout_time default sysdate;

Table altered.

SQL> insert into test (login_time) values (date '2021-01-23');  --> no logout time here

1 row created.

SQL> select * from test;

LOGIN_TIME          LOGOUT_TIME
------------------- -------------------
2021-08-20 09:57:00 2021-08-20 10:30:00
2021-08-20 10:00:00 2021-08-20 18:58:15
2021-08-20 08:00:00 2021-08-20 18:58:15
2021-01-23 00:00:00 2021-08-20 18:58:44   --> logout time is automatically inserted

SQL>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多