开发者

Need help with syntax for test ISNUMERIC in case

I am trying to stop input of negative values to process in any of the ranges, rather if they are negative they should drop down to the end of the 1st case statement as 'Invalid'. This is not working as when I run a test against the input of (-1000) i get a row for <=50K. I am afraid my syntax is wrong, but not sure why.

ALTER FUNCTION [dbo].[FN_1ST_UPB_Bands]
(
    @FN_1ST_UPB_Band            int
     )

RETURNS varchar(16)
AS
BEGIN

declare @Return varchar (16)

select @Return =

        Case
           When ISNUMERIC(@FN_1ST_UPB_Band)= 1 then  

        case 
            When @FN_1ST_UPB_Band is NULL then  '    Missing'
            When @FN_1ST_UPB_Band = 0 then '  0'
            When @FN_1ST_UPB_Band < = 50000 then ' <=50K'
            When @FN_1ST_UPB_Band between 50000 and 100000 then ' 50-100'
            When @FN_1ST_UPB_Band between 100000 and 150000 then '100-150'
            When @FN_1ST_UPB_Band between 150000 and 200000 then '150-200'
    开发者_如何学C        When @FN_1ST_UPB_Band between 200000 and 250000 then '200-250'
            When @FN_1ST_UPB_Band between 250000 and 300000 then '250-300'
            When @FN_1ST_UPB_Band between 300000 and 350000 then '300-350'
            When @FN_1ST_UPB_Band between 350000 and 400000 then '350-400'
            When @FN_1ST_UPB_Band between 400000 and 450000 then '400-450'
            When @FN_1ST_UPB_Band between 450000 and 500000 then '450-500'
            When @FN_1ST_UPB_Band > 500000 then '500K+'
            else null End
         else 'Invalid' End

    RETURN @return
End


ISNUMERIC is always true on an INT field, you can safely throw it away.

Try this:

declare @FN_1ST_UPB_Band INT
SET  @FN_1ST_UPB_Band = -1000

select  case 
            When @FN_1ST_UPB_Band is NULL then  '    Missing'
            When @FN_1ST_UPB_Band < 0 Then 'Invalid'
            When @FN_1ST_UPB_Band = 0 then '  0'
            When @FN_1ST_UPB_Band < = 50000 then ' <=50K'
            When @FN_1ST_UPB_Band between 50000 and 100000 then ' 50-100'
            When @FN_1ST_UPB_Band between 100000 and 150000 then '100-150'
            When @FN_1ST_UPB_Band between 150000 and 200000 then '150-200'
            When @FN_1ST_UPB_Band between 200000 and 250000 then '200-250'
            When @FN_1ST_UPB_Band between 250000 and 300000 then '250-300'
            When @FN_1ST_UPB_Band between 300000 and 350000 then '300-350'
            When @FN_1ST_UPB_Band between 350000 and 400000 then '350-400'
            When @FN_1ST_UPB_Band between 400000 and 450000 then '400-450'
            When @FN_1ST_UPB_Band between 450000 and 500000 then '450-500'
            When @FN_1ST_UPB_Band > 500000 then '500K+'
            else null
        End


in that case (no pun intended)

do this

When @FN_1ST_UPB_Band < 0 Then 'Invalid'
When @FN_1ST_UPB_Band >= 0 and @FN_1ST_UPB_Band < = 50000 then ' <=50K'

because -1000 is less than 50000


Because they are evaluated in order, -1000 is <= 50000, so it evaluates as true and returns the "<= 50000". You need between 1 and 49999 instead (Not 50000 as you had 50000 would evalute true in 2 cases).


Well isn't -1000 less than 50000? Don't you need that case to be:

When @FN_1ST_UPB_Band between 0 and 49999 then ' <=50K'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜