【问题标题】:Use converted date in Where clause using dynamic date statements使用动态日期语句在 Where 子句中使用转换日期
【发布时间】:2020-08-24 10:36:35
【问题描述】:

我已成功地将存储为 varchar 的日期转换为有效日期,现在需要在 Where 子句的动态日期语句中使用它。不幸的是,我不断收到错误,或者没有回报。下面的代码运行,但我没有得到任何回报,如果我输入一个静态日期,它可以工作,如果我在 where 子句中使用“to_date(a.sold_dt,'YYYYMMDD')”,它就会出错。

select
to_date(a.sold_dt,'YYYYMMDD') as Sold_dt,
a.BUYER_ZIP_CD,
b.Statename,
b.dmaname,

COUNT(VIN) VIN_COUNT

from MyTable a
join OtherTable b
on a.Buyer_Zip_cd = b.zipcode

where sold_dt >= (Current_Date() -92)
or sold_dt (between (current_date() -457) and (current_date() - 367))
and sale_type = 'Retail'

group by 1,2,3,4

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: sql date dynamic snowflake-cloud-data-platform to-date


【解决方案1】:

我不知道您的所有语法是否都适用于您正在使用的数据库。但这绝对是错误的:

where sold_dt >= (Current_Date() -92) or
      sold_dt (between (current_date() -457) and (current_date() - 367)) and
--------------^ -------------------------------------------------------^
      sale_type = 'Retail'

在我知道的任何数据库中,between 周围都不允许使用括号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2019-07-15
    相关资源
    最近更新 更多