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

问题描述 投票:0回答:1

我已经成功地将一个存储为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 to-date
1个回答
0
投票

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

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

Parens是不允许用在 between 在我所知道的任何数据库中。

© www.soinside.com 2019 - 2024. All rights reserved.