我想选择所有列但是如果fuel = 1则开始日期> '1 oct 2013'否则,如果燃料= 0,则开始日期> '九月一日'。
使用SQL Server 2005。
我试过了..
SELECT * FROM EntitlementEpisode
WHERE
IF EnFuel = 1
THEN
IF
BEGIN
[EnEpisodeStart] >
(SELECT TOP 1 [BeYearStart]
FROM [BenefitYear] ORDER BY [BeId] desc)
END
ELSE IF EnFuel = 0
THEN
IF
BEGIN
[EnEpisodeStart] >
CONVERT(VARCHAR(30), '1 OCT' + YEAR(GETDATE()))
END
这是正确的解决方法吗?
答案 0 :(得分:3)
使用二元运算符的强度......!
SELECT *
FROM EntitlementEpisode
WHERE
(EnFuel=1 AND EnEpisodeStart] >
(SELECT TOP 1 [BeYearStart]
FROM [BenefitYear] ORDER BY [BeId] desc))
OR
(EnFuel=0 AND [EnEpisodeStart] >
CONVERT(VARCHAR(30), '1 OCT' + YEAR(GETDATE())))
答案 1 :(得分:0)
像
这样的东西SELECT *
FROM EntitlementEpisode
WHERE (EnFuel = 1 AND [EnEpisodeStart] > (SELECT TOP 1 [BeYearStart] FROM [BenefitYear] ORDER BY [BeId] desc)
OR (EnFuel = 0 AND [EnEpisodeStart] > CONVERT(VARCHAR(30), '1 OCT' + YEAR(GETDATE())))