Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

sql-server - T-SQL CASE语句返回不同的过滤器(T-SQL CASE statement returning a different filter)

I have a column named TimeSlots that holds a twelve hour range of dates, 7:00:00 thru 19:00:00 .

(我有一个名为列TimeSlots保存日期的十二时段范围, 7:00:00直通19:00:00 。)

I have a stored procedure that accepts a varchar parameter that could be 'AM' or 'PM'.

(我有一个存储过程,该存储过程接受可以为'AM'或'PM'的varchar参数。)

I am using a CASE expression inside the WHERE to get either:

(我在WHERE中使用CASE表达式来获取以下任一信息:)

  • Time-Slots < 12:00:00 for AM times

    (AM Time-Slots < 12:00:00)

  • TimeSlots >= '12:00:00' for PM times.

    (下午时间TimeSlots >= '12:00:00'的时间段。)

It ain't working.

(它没有用。)

Following is the example that i tried:

(以下是我尝试的示例:)

AND [TimeSlot] = CASE WHEN @ApptTime = 'AM' THEN [TimeSlot] < '12:00:00'
                                 WHEN @ApptTime = 'PM' THEN [TimeSlot] >= '12:00:00'

How can this be done?

(如何才能做到这一点?)

  ask by StanB13 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's not how CASE expressions (not statements) work.

(那不是CASE表达式(不是语句)的工作方式。)

They can only evaluate to a single value.

(他们只能求值一个值。)

You can just use Boolean expressions to do what you want.

(您可以只使用布尔表达式执行所需的操作。)

...
AND (@ApptTime = 'AM'
     AND [TimeSlot] < '12:00:00'
      OR @ApptTime = 'PM'
         AND [TimeSlot] >= '12:00:00')
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...