Amazon Redshift - 条件变量
Amazon Redshift - variable in where condition
我正在尝试自动执行查询过滤器。所以,过滤器的值每个月都在变化。
Select * 来自 table where desc like '202012%'
以下命令让我得到 202012%,但我无法弄清楚 if/how我可以在 where 子句中使用它
concat(提取(current_date的年份), lpad(提取(current_date的月份), 2)) || '%'
谢谢。
你要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
是语言关键字,因此不是列名的好选择。我在代码片段中将其重命名为 descr
。
我正在尝试自动执行查询过滤器。所以,过滤器的值每个月都在变化。
Select * 来自 table where desc like '202012%'
以下命令让我得到 202012%,但我无法弄清楚 if/how我可以在 where 子句中使用它 concat(提取(current_date的年份), lpad(提取(current_date的月份), 2)) || '%'
谢谢。
你要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
是语言关键字,因此不是列名的好选择。我在代码片段中将其重命名为 descr
。