date参数类似于SQL Server中的访问

时间:2014-04-28 21:29:43

标签: sql sql-server datetime

我正在处理一个查询,我们正在从MS Access中的一个更简单的查询扩展。在Access查询中,日期值为REQUIREMENTS_DATE < Now()-([M: 56 T:57 W:58 Th: 59 F:60]),我在其中REQUIREMENTS_DATE < GETDATE() - 60。访问查询允许运行报表的人根据星期几输入给定的天数,但到目前为止,我必须手动更改SQL代码,这对我来说不是什么大问题,但我会喜欢将它导出到Excel,并且可以执行具有很少SQL知识的用户。 SQL服务器中是否有类似于Access中使用的函数,以便我的用户可以输入一个值并让报告在该日期运行:

SELECT                  P.PLANT_CODE,
                P.STOCK_CATEGORY,
                P.MATERIAL,
                P.DISTRIBUTION_VERSION_CODE,
                P.PERIOD_CODE,
                P.REQUIREMENTS_DATE,
                P.PLANNED_QTY AS 'REM PLAN QTY',
                P.VERSION_IND_FLAG,
                P.SIZE_LITERAL,
                P.WITHDRAWN_QUANTITY,
                P.TECHNICAL_INDEX,
                P.PLANNED_QTY + P.WITHDRAWN_QUANTITY AS 'ORIGINAL FCST QTY'
 FROM           VW_PLANNED_REQMNTS_TXT P
 WHERE          P.PLANT_CODE IN ('6040','6041')
 AND            P.STOCK_CATEGORY IN ('A60385000','A60385003')
 AND            P.DISTRIBUTION_VERSION_CODE IN ('00','01','ZU','Z2')
 AND            P.REQUIREMENTS_DATE < GETDATE() - 60
 AND            P.PLANNED_QTY

1 个答案:

答案 0 :(得分:0)

尝试运行:

declare @number as int
set @number =   CASE
                WHEN DATENAME(dw,GETDATE()) = 'Monday'
                THEN 56
                WHEN DATENAME(dw,GETDATE()) = 'Tuesday'
                THEN 57
                WHEN DATENAME(dw,GETDATE()) = 'Wednesday'
                THEN 58
                WHEN DATENAME(dw,GETDATE()) = 'Thursday'
                THEN 59
                WHEN DATENAME(dw,GETDATE()) = 'Friday'
                THEN 60
                END;


SELECT          P.PLANT_CODE,
                P.STOCK_CATEGORY,
                P.MATERIAL,
                P.DISTRIBUTION_VERSION_CODE,
                P.PERIOD_CODE,
                P.REQUIREMENTS_DATE,
                P.PLANNED_QTY AS 'REM PLAN QTY',
                P.VERSION_IND_FLAG,
                P.SIZE_LITERAL,
                P.WITHDRAWN_QUANTITY,
                P.TECHNICAL_INDEX,
                P.PLANNED_QTY + P.WITHDRAWN_QUANTITY AS 'ORIGINAL FCST QTY'
 FROM           VW_PLANNED_REQMNTS_TXT P
 WHERE          P.PLANT_CODE IN ('6040','6041')
 AND            P.STOCK_CATEGORY IN ('A60385000','A60385003')
 AND            P.DISTRIBUTION_VERSION_CODE IN ('00','01','ZU','Z2')
 AND            P.REQUIREMENTS_DATE < GETDATE() - @number
 AND            P.PLANNED_QTY;