【问题标题】:How to convert DATE_TRUNC to MariaDB? [duplicate]如何将 DATE_TRUNC 转换为 MariaDB? [复制]
【发布时间】:2019-05-07 00:12:37
【问题描述】:

我在 PostgreSQL 中有以下 select 语句:

DATE_TRUNC('{{time_interval}}',u.created_at) AS "Signup Date",

其中 {{time_internal}} 是设置为 month 的变量

我需要将该行转换为 MariaDB,这是我目前所拥有的:

DATE_FORMAT(CURDATE(), u.created_at) AS "Signup Date",

这没有按预期输出结果,让我相信我错误地迁移了这行 SQL。

将这行 SQL 从 PostgreSQL 迁移到 MariaDB 时我做错了什么?

【问题讨论】:

  • 是这样吗? DATE_FORMAT(u. created_at, '%Y-%m-01') AS "Signup Date",
  • 上一题我给你的方法有什么问题?即DATE_FORMAT(u.created_at, '%Y-%m-01 00:00:00')?如果您实际上不需要时间,则可以跳过 00:00:00 部分

标签: sql postgresql mariadb


【解决方案1】:

我认为你需要使用case 声明:

select (case when @timeinterval = 'day'
             then date(u.created_at)
             when @timeinterval = 'month'
             then u.created_at + interval (1 - day(u.created_at)) day
             when @timeinterval = 'year'
             then makedate(year(u.created_at), 1)
        end) as Signup_Date

还有其他可能性,但'day''month''year' 是最常见的日期部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 2021-10-01
    • 2016-01-04
    • 2021-08-18
    • 2013-11-26
    • 2011-08-31
    • 2016-04-16
    • 2012-04-11
    相关资源
    最近更新 更多