How can I use the when-between expression in sql?

This query gives me a syntax error in the time line. how can i solve them?

alter FUNCTION [dbo].[fn_GetActivityLogsArranger]
(
@time AS nvarchar(max)
)

RETURNS  nvarchar(max)
AS
BEGIN
declare @Return varchar(30)

select @Return = case @time
when between '15:00' and '15:30' then '15:00-15:30'
when between '15:30' and '16:00' then '15:30-16:00'
when between '16:00' and '16:30' then '16:00-16:30'
when between '16:00' and '16:30' then '16:00-16:30' 
when between '16:30' and '17:00' then '16:30-17:00' 
when between '17:00' and '17:30' then '17:00-17:30' 
when between '17:30' and '18:00' then '17:30-18:00'
else 'Unknown'
 Return @Return
end
+3
source share
6 answers
alter FUNCTION [dbo].[fn_GetActivityLogsArranger]
(
    @time AS varchar(30)
)
RETURNS  
varchar(30)AS
BEGIN
declare @Return varchar(30)
select @Return = case 
when @time between '15:00' and '15:30' then '15:00-15:30'
when @time between '15:30' and '16:00' then '15:30-16:00'
when @time between '16:00' and '16:30' then '16:00-16:30'
when @time between '16:00' and '16:30' then '16:00-16:30' 
when @time between '16:30' and '17:00' then '16:30-17:00' 
when @time between '17:00' and '17:30' then '17:00-17:30'
when @time between '17:30' and '18:00' then '17:30-18:00'
else 'Unknown' 
end
Return @Return
end
+4
source

You cannot use this case syntax format. You will need to make a case that checks:

select @Return = case 
when @time between '15:00' and '15:30' then '15:00-15:30'
when @time between '15:30' and '16:00' then '15:30-16:00'
when @time between '16:00' and '16:30' then '16:00-16:30'
when @time between '16:00' and '16:30' then '16:00-16:30' 
when @time between '16:30' and '17:00' then '16:30-17:00' 
when @time between '17:00' and '17:30' then '17:00-17:30' 
when @time between '17:30' and '18:00' then '17:30-18:00'
else 'Unknown' END

Return @Return

Also, you were missing END at the end of your case statement (see END in upper case above).

+4
source

:    WHEN Boolean_expression THEN result_expression [... n]    [ELSE else_result_expression] END

+1

@variable ,

select @Return = case 
when @time between ('15:00' and '15:30') then '15:00-15:30'
when @time between ('15:30' and '16:00') then '15:30-16:00'
when @time between ('16:00' and '16:30') then '16:00-16:30'
when @time between ('16:00' and '16:30') then '16:00-16:30' 
when @time between ('16:30' and '17:00') then '16:30-17:00' 
when @time between ('17:00' and '17:30') then '17:00-17:30' 
when @time between ('17:30' and '18:00') then '17:30-18:00'
else 'Unknown'
0

You need to have a variable in every WHEN clause for example.

case 
    when @time between '15:00' and '15:30' then '15:00-15:30'
    when @time between '15:30' and '16:00' then '15:30-16:00'
0
source

You should not use this function, you should have a table or table function of temporary buckets so that you can make a pure join against it. See my other post for an example. The join will far exceed the approach of calling your function in a rowset.

0
source

Source: https://habr.com/ru/post/1707746/


All Articles