我有一个FirstRentPaymentDate日期字段和一个RentFrequency字段,其中包含四个可能的值(Week,Fortnight,Month,Year)。
日期字段上的时间无关紧要,但都设置为12:00:00。
如果频率的FirstRentPaymentDate使用SQL查询等于今天,我怎么能算出来?
答案 0 :(得分:1)
执行此操作的一种方法不需要使用lastpaid日期:
select
firstdate
,frequency
from sampledata
WHERE
CASE WHEN frequency = 'week' THEN DATEDIFF(DD,firstdate,getdate()) % 7
WHEN frequency = 'fortnight' THEN DATEDIFF(DD,firstdate,getdate()) % 14
WHEN frequency = 'month' AND Day(firstdate) = Day(getdate()) THEN 0
WHEN frequency = 'year' AND Day(firstdate) = Day(getdate())
AND Month(firstdate) = Month(getdate()) THEN 0
END = 0