【问题标题】:Amazon Redshift - variable in where conditionAmazon Redshift - 在 where 条件下的变量
【发布时间】:2020-12-07 21:30:11
【问题描述】:

我正在尝试自动化查询过滤器。所以,过滤器的值每个月都在变化。

Select * from table where desc like '202012%'

以下命令让我获得 202012%,但我无法弄清楚是否/如何在 where 子句中使用它 concat(从 current_date 中提取(year), lpad(extract(从 current_date 中的月份), 2)) || '%'

谢谢。

【问题讨论】:

    标签: sql string datetime amazon-redshift where-clause


    【解决方案1】:

    你想要to_char()吗?

    where descr like to_char(current_date, 'YYYYMM') || '%'
    

    重要提示:您的问题表明您将日期存储为字符串。不!使用正确的数据类型来存储您的数据:这样更有效,也更安全(保证数据完整性)。然后,您可以使用日期函数,如下所示:

    where descr >= date_trunc('month', current_date) 
      and descr <  dateadd(month, 1, date_trunc('month', current_date))
    

    旁注:desc 是语言关键字,因此不是列名的好选择。我在代码 sn-ps 中将其重命名为descr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      相关资源
      最近更新 更多