使用动态日期语句在Where子句中使用转换后的日期

时间:2020-05-08 19:15:36

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

我已经成功地将存储为varchar的日期转换为有效日期,现在需要在Where子句中的动态date语句中使用它。不幸的是,我不断收到错误,或者没有回报。下面的代码可以运行,但是我没有任何回报,如果我输入一个静态日期,它可以工作,并且如果我在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

1 个答案:

答案 0 :(得分:0)

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

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

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