[业务从每年的10月1日开始。我需要两个查询,一个查询返回当前营业年度的所有记录,另一个查询返回上一个营业年度的所有记录。在这种情况下,year年并不重要。
我写过:
strSql1 = "SELECT * FROM Orders WHERE DateDiff('d',
[OrderDate], [YearStart])<=365 ORDER BY [OrderDate] ASC"
Me.frmCurrentYear.Form.RecordSource = strSql1
strSql2 = "SELECT * FROM Orders WHERE DateDiff('d', [OrderDate],
[YearStart])>365 AND DateDiff('d', [PurchaseDate],[YearStart]<=730) ORDER BY
[OrderDate] ASC"
Me.frmPreviousYear.Form.RecordSource = strSql2
问题:
从我的代码中,我将不得不向表中添加YearStart
字段。有办法避免这种情况吗?
使用BETWEEN
会更有效吗?
谢谢。
1-您总是可以计算出例如月份是oct,nov或dec,然后是当前年份,否则是上一年。
case when Month(GetDate()) >=10 then Cast('10-01'+Cast(Year(GetDate() as Varchar(5)) as date)
when Month(GetDate()) <10 then Cast('10-01'+Cast(Year(GetDate())-1 as varcher(5)) as date)
END as YearStart
将DateSerial用于此类任务:
strSql1 = "SELECT * FROM Orders " & _
"WHERE DateDiff('m', [OrderDate], DateSerial(Year(Date()), 1, 1) Between -3 And 9 " & _
"ORDER BY [OrderDate] ASC"
strSql2 = "SELECT * FROM Orders " & _
"WHERE DateDiff('m', [OrderDate], DateSerial(Year(Date()) - 1, 1, 1) Between -3 And 9 " & _
"ORDER BY [OrderDate] ASC"