帮我在SQL Server 2008 r2中执行一项功能 [英] Help me a function in SQL Server 2008 r2
本文介绍了帮我在SQL Server 2008 r2中执行一项功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含此字段的表格CA:ID_CA,CA_NAME,TimeStart,TimeEnd
如果TimeStart和TimeEnd在'6:00:00 P之间,我想将CA_NAME设置为'DAY'到CA .M'和'5:59:59 A.M',如果TimeStart和TimeEnd介于'6:00:00 A.M'和'5:59:59 P.M'之间,则CA_NAME为'NIGHT'。
我尝试但没有运气。谢谢!
I have a table CA with this field: ID_CA, CA_NAME, TimeStart, TimeEnd
I want to set CA_NAME is 'DAY' into CA if TimeStart and TimeEnd between '6:00:00 P.M' and '5:59:59 A.M', CA_NAME is 'NIGHT' if TimeStart and TimeEnd between '6:00:00 A.M' and '5:59:59 P.M'.
I try but no luck. Thanks you!
Declare @TimeStamp Datetime, @Result int
If @TimeStamp in ('6:00:00 P.M' and '5.59.59 A.M') ==> Return @Result = 1 (It's mean true)
else return Result = 0 (It's mean flase)
然后,我可以使用此功能确定DAY或NIGHT。
谢谢你!
Then, i can use this function to detemine DAY or NIGHT.
Thanks You !
推荐答案
使用 DatePart函数 [ ^ ]获取小时组件并将其与边框进行比较值:
Use DatePart function[^] to get the hour component and compare that to your border values:
select case when datepart(hour, @TimeStamp) >= 6 and datepart(hour, @TimeStamp) < 18 then 'Day' else 'Night' end
这篇关于帮我在SQL Server 2008 r2中执行一项功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文