0

取消了我原来的问题,因为这可能是一个更清晰的版本!

这就是我想要做的。目前,我得到 *'当 EXISTS 未引入子查询时,选择列表中只能指定一个表达式。强调文字*

declare @X int

SET @X=(
SELECT B.ID, 
Case
When Getdate() Between '2012-11-01 00:00' AND '2013-10-31 23:59' then 731
When Getdate() Between '2012-11-01 00:00' AND '2013-10-31 23:59' then 1096
END FROM  Booking B)


Select 
B.Created - @X AS 'Bookings date less X'
From Booking B
4

2 回答 2

2

不确定您要如何处理那里的第二个位,但这应该可以帮助您处理第一个(假设您使用的是 SQL Server)...

declare @x integer;

set @x = case WHEN Getdate() Between '2012-11-01 00:00' AND '2013-10-31 23:59' then -731 when Getdate() Between '2013-11-01 00:00' AND '2014-10-31 23:59' then 1 end;

print @x
于 2013-09-06T16:02:10.977 回答
1

如果我正确理解您的问题,您正在寻找case statementvariable

DECLARE @X int

SELECT @X = 
    CASE WHEN Getdate() Between '2012-11-01 00:00' AND '2013-10-31 23:59' THEN -731
    ELSE
    CASE WHEN Getdate() Between '2013-11-01 00:00' AND '2014-10-31 23:59' THEN 1096
    ELSE
    NULL
    END

然后在随后的一些声明中:

CASE WHEN B.Created < @X THEN 'Hello' ELSE 'Not Hello' END
于 2013-09-06T16:03:38.150 回答