需要有关测试 ISNUMERIC 语法的帮助,以防万一

发布于 2024-09-08 12:09:43 字数 1390 浏览 2 评论 0原文

我试图停止输入负值以在任何范围内进行处理,而不是如果它们是负值,则它们应该下降到第一个 case 语句的末尾作为“无效”。这不起作用,因为当我对 (-1000) 的输入运行测试时,我得到一行 <=50K。我担心我的语法是错误的,但不知道为什么。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落墨 2024-09-15 12:09:43

ISNUMERICINT 字段上始终为 true,您可以放心地丢弃它。

试试这个:

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

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
厌倦 2024-09-15 12:09:43

在这种情况下(没有双关语)

这样做是

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

因为 -1000 小于 50000

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

箜明 2024-09-15 12:09:43

因为它们是按顺序求值的,所以 -1000 是 <= 50000,因此它求值为 true 并返回“<= 50000”。您需要 1 到 49999 之间的值(不是 50000,因为您有 50000 在 2 种情况下会评估 true)。

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

雨后彩虹 2024-09-15 12:09:43

那么-1000不是小于50000吗?您不需要这种情况吗:

当 @FN_1ST_UPB_Band 在 0 和 49999 之间时,则 ' <=50K'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文