【问题标题】:How to get dates of current month or as specified month using sqlite query?如何使用 sqlite 查询获取当前月份或指定月份的日期?
【发布时间】:2013-10-21 09:18:32
【问题描述】:

在我的应用程序中,我想使用当月的所有日期,我必须在其他查询中使用这些日期才能加入。

如果我可以通过传递月份编号来获取指定月份的日期,那么我也没有任何问题。

我需要使用 sqlite 数据库进行查询。 任何帮助将不胜感激。

【问题讨论】:

  • 请展示您首先说的数据库和表格。
  • @MajidDaeiNejad:我们不需要数据库或表,我们可以通过内置函数来完成。

标签: android ios sql sqlite


【解决方案1】:

替代浓缩解决方案:

SELECT DATE(JULIANDAY('NOW', 'START OF MONTH')+d1*9+d2*3+d3) AS Days FROM (
    SELECT 0 AS d1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
) JOIN (
    SELECT 0 AS d2 UNION SELECT 1 UNION SELECT 2
) JOIN (
    SELECT 0 AS d3 UNION SELECT 1 UNION SELECT 2
) WHERE STRFTIME('%m', Days)=STRFTIME('%m', 'NOW') ORDER BY Days;

请注意,此解决方案需要比 @CL 更多的计算。

【讨论】:

    【解决方案2】:

    引用my previous answer

    SELECT date('now', 'start of month') AS Date
    UNION ALL
    SELECT date('now', 'start of month', '+1 days')
    UNION ALL
    SELECT date('now', 'start of month', '+2 days')
    UNION ALL
    ...
    UNION ALL
    SELECT date('now', 'start of month', '+27 days')
    UNION ALL
    SELECT date('now', 'start of month', '+28 days') WHERE strftime('%m', 'now', 'start of month', '+28 days') = strftime('%m', 'now')
    UNION ALL
    SELECT date('now', 'start of month', '+29 days') WHERE strftime('%m', 'now', 'start of month', '+29 days') = strftime('%m', 'now')
    UNION ALL
    SELECT date('now', 'start of month', '+30 days') WHERE strftime('%m', 'now', 'start of month', '+30 days') = strftime('%m', 'now')
    

    【讨论】:

    • 嗨 CL 我可以得到指定月份的日期吗?我必须为此做出哪些改变。
    • 'now', 'start of month' 替换为该月初的日期,例如'2013-10-01'
    • 我想,我必须创建一个函数来传递月初的日期。目前,我已根据您的查询创建了一个视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    相关资源
    最近更新 更多