gpt4 book ai didi

SQL Server Case 语句不过滤记录

转载 作者:行者123 更新时间:2023-12-01 12:26:01 25 4
gpt4 key购买 nike

据我所知,case 语句不会过滤记录。一位开发人员坚持认为它确实......

我想限制具有日期范围的记录数,但这段代码没有这样做。

declare @ClientId [int],
@EventStartDate DateTime,
@EventEndDate DateTime

set @ClientId = 1
set @EventStartDate = '2016-05-23'
set @EventEndDate = '2016-09-08'

SELECT
[CalendarEventId], [ClientId],
[EventDate],
[Title],
[EventText],
[CreatedBy], [CreatedDate],
[ModifiedBy], [ModifiedDate],
CASE
WHEN [EventDate] >= @EventStartDate
AND EventDate <= @EventEndDate
THEN 'ACTIVE'
ELSE 'NOT ACTIVE'
END AS [STATUS]
FROM
[dbo].[CalendarEvent]
WHERE
ClientId = @ClientId
ORDER BY
EventDate

最佳答案

您关于代码不按日期过滤的断言是正确的,但您的 friend 错了。

如果您也想按日期范围进行过滤,则需要将 where 子句更改为:

WHERE ClientId = @ClientId 
AND EventDate >= @EventStartDate
AND EventDate <= @EventEndDate

查询中的 case 表达式所做的唯一事情就是有条件地设置结果集中列的输出。

关于SQL Server Case 语句不过滤记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com