我正在尝试根据case语句更改where子句的条件。问题是它不喜欢'之间'声明。我可以在那里使用案例更改“Where”标准吗?这是导致问题的原因(关键字'之间'附近的语法不正确。) 我想在日期范围内从PMF_FISCALCALENDAR表中获取会计月份。问题是一个日期范围可能是一个无效的数字,在这种情况下我需要使用第二个日期。
关键字'between'附近的语法不正确。
,(select PERIOD =
CASE
WHEN PERIOD = '1' THEN
'Jan'
WHEN Period = '2' THEN
'Feb'
WHEN PERIOD = '3' THEN
'Mar'
WHEN Period = '4' THEN
'Apr'
WHEN PERIOD = '5' THEN
'May'
WHEN Period = '6' THEN
'Jun'
WHEN PERIOD = '7' THEN
'Jul'
WHEN Period = '8' THEN
'Aug'
WHEN PERIOD = '9' THEN
'Sep'
WHEN Period = '10' THEN
'Oct'
WHEN PERIOD = '11' THEN
'Nov'
WHEN Period = '12' THEN
'Dec'
END
FROM PMF_FISCALCALENDAR
WHERE
(CASE
WHEN YEAR(SALESQUOTATIONLINE.KTI_SENTDATE) = 1900 Then
SALESQUOTATIONLINE.KTI_SENTDATE between PMF_FISCALCALENDAR.STARTDATE and PMF_FISCALCALENDAR.ENDDATE
ELSE
SALESQUOTATIONTABLE.PMF_QUOTESENTDATE between PMF_FISCALCALENDAR.STARTDATE and PMF_FISCALCALENDAR.ENDDATE
END)
) AS 'Fiscal Month Sent'
我想使用一个或另一个案例结果作为where子句标准。 我正在使用SQL SERVER 2005
答案 0 :(得分:3)
将语句更改为
WHERE
CASE
WHEN YEAR(SALESQUOTATIONLINE.KTI_SENTDATE) = 1900 Then
SALESQUOTATIONLINE.KTI_SENTDATE
ELSE
SALESQUOTATIONTABLE.PMF_QUOTESENTDATE
END between PMF_FISCALCALENDAR.STARTDATE and PMF_FISCALCALENDAR.ENDDATE
答案 1 :(得分:0)
尝试使用此方法以获得更好的语法和性能:
SELECT PERIOD = convert(char(3), dateadd(m, PERIOD, -1), 0)
FROM PMF_FISCALCALENDAR
WHERE
SALESQUOTATIONLINE.KTI_SENTDATE < '1901-01-01' AND
SALESQUOTATIONLINE.KTI_SENTDATE >= '1900-01-01' AND
SALESQUOTATIONLINE.KTI_SENTDATE between PMF_FISCALCALENDAR.STARTDATE and PMF_FISCALCALENDAR.ENDDATE
or
(SALESQUOTATIONLINE.KTI_SENTDATE < '1900-01-01' or
SALESQUOTATIONLINE.KTI_SENTDATE >= '1901-01-01' or
SALESQUOTATIONLINE.KTI_SENTDATE is null) AND
SALESQUOTATIONTABLE.PMF_QUOTESENTDATE between PMF_FISCALCALENDAR.STARTDATE and PMF_FISCALCALENDAR.ENDDATE