【问题标题】:How to change the field datetime without time in database H2如何在数据库 H2 中更改没有时间的字段日期时间
【发布时间】:2019-08-07 14:02:28
【问题描述】:

我有一张桌子:

CREATE TABLE `operation` (
  `id` bigint(20) NOT NULL,
  `start_time` datetime DEFAULT NULL,
  `finish_time` datetime NOT NULL
);

对于这个表,你需要插入从finish_time到start_time的值。通过在没有时间的情况下转换一个字段。

例如:“2018-02-02 10:10:10” => “2018-02-02 00:00:00”

我知道如何为 MySQL 做到这一点:

update operation
set start_time = DATE_FORMAT(finish_time,'%y-%m-%d')
where start_time is null;

但是H2中没有DATE_FORMAT这个函数。

如何对数据库 H2 执行此查询?

【问题讨论】:

  • start_time的数据类型不应该是DATE而不是DATETIME吗?

标签: database h2


【解决方案1】:

您可以使用CAST() 剥离时间信息。例如:

update operation
set start_time = cast(finish_time as date)
where start_time is null;

【讨论】:

    【解决方案2】:

    您可以尝试使用 FORMATDATETIME()

    update operation
    set start_time = FORMATDATETIME((finish_time, '%y-%m-%d')
    where start_time is null;
    

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多