views:

35

answers:

4

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'
            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
+2  A: 

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
Quassnoi
Hi Leslie, no it has to output as '<=0' per the requestor, got it to work by changing the band from When @FN_1ST_UPB_Band = 0 then ' 0' to When @FN_1ST_UPB_Band = 0 then ' 0'When @FN_1st_UPB_BAND < = 0 then ' <=0'
JMS49
@JMS49: My name is Alex.
Quassnoi
Quassnoi, I am sorry there was a comment from Leslie initially with a similar reply. That was what I was originally replying to. Please accept my apologies. The boss wants the negative numbers in the same bucket as zeros, hence I used the statement When @FN_1st_UPB_BAND < = 0 then ' <=0'. I was over thinking it the first time around.
JMS49
A: 

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

SQLMenace
A: 

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).

DaveShaw
A: 

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'

Leslie