利用子字符串和 isnumeric 的 T-SQL Case 语句

发布于 2024-09-19 02:02:14 字数 878 浏览 3 评论 0原文

我有一个 T-SQL 存储过程,它向 .NET 页面上的网格提供大量数据......以至于我将选项放在页面顶部的“0-9”和每个字母字母表,以便当用户单击该字母时,我想根据以第一个字母开头的结果过滤结果。假设我们正在使用产品名称。因此,如果用户单击“A”,我只希望我的存储过程返回 SUBSTRING(ProductName, 1, 1) =“A”的结果。

我所困扰的是以数字开头的产品名称。在这种情况下,我想获取 ISNUMERIC(SUBSTRING(ProductName, 1, 1)) = 1 处的所有 ProductName 值。我使用名为 @FL 的输入参数。 @FL 要么是零(我们很少有以数字开头的产品,所以我以这种方式将它们全部放在一起)。

当然,还有 WHERE SUBSTRING(ProductName, 1, 1) IN ('0', '1', '2'.....) 的替代方案,但即便如此,我也从未能够设计出 CASE 语句这将对一个评估执行 = 操作,对另一个评估执行 IN 语句。

这是我的 WHERE 子句的 CASE 部分的过程。它不起作用,但如果仅从伪代码的角度来看,它可能很有价值。

预先感谢您的任何想法。

AND CASE @FL
    WHEN "0" THEN
    CASE WHEN @FL = "0" THEN
        isnumeric(substring(dbo.AssnCtrl.Name, 1, 1)) = 1
    ELSE
        SUBSTRING(dbo.AssnCtrl.Name, 1, 1) = @FL            
    END
END

*** 我知道 CASE 语句的这种使用是“非标准的”,但我在网上找到了它并认为它有一些希望。但尝试使用单个 CASE 语句会产生相同的结果(“=”附近的错误)。

I have a T-SQL stored proc that supplies a good amount of data to a grid on a .NET page....so much so that I put choices at the top of the page for "0-9" and each letter in the alphabet so that when the user clicks the letter I want to filter my results based on results that begin with that first letter. Let's say we're using product names. So if the user clicks on "A" I only want my stored proc to return results where SUBSTRING(ProductName, 1, 1) = "A".

Where I'm getting hung up is on product names that begin with a number. In that case I want to fetch all ProductName values where ISNUMERIC(SUBSTRING(ProductName, 1, 1)) = 1. I'm using an input parameter called @FL. @FL will either be a zero (we have few products that begin with numerics, so I lump them all together this way).

Of course there's also the alternative of WHERE SUBSTRING(ProductName, 1, 1) IN ('0', '1', '2'.....) but even then, I've never been able to devise a CASE statement that will do an = on one evaluation and an IN statement for the other.

Here's what I have in my proc for the CASE part of my WHERE clause. It doesn't work, but it may be valuable if only from a pseudocode standpoint.

Thanks in advance for any ideas you may have.

AND CASE @FL
    WHEN "0" THEN
    CASE WHEN @FL = "0" THEN
        isnumeric(substring(dbo.AssnCtrl.Name, 1, 1)) = 1
    ELSE
        SUBSTRING(dbo.AssnCtrl.Name, 1, 1) = @FL            
    END
END

*** I know that this use of the CASE statement is "non-standard", but I found it online and thought it had some promise. But attempts to use a single CASE statement yielded the same result (an error near '=').

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

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

发布评论

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

评论(3

生寂 2024-09-26 02:02:14

为什么不直接使用 Like 运算符?

   Where dbo.AssnCtrl.Name Like @FL + '%' 

当他们选择“任意数字”选项时,将 @FL 作为“[0-9]”传递

(我假设您在此名称列上有索引?)

Why not just use Like operator ?

   Where dbo.AssnCtrl.Name Like @FL + '%' 

When they select the Any Number option, pass in @FL as '[0-9]'

(I assume you have an index on this name column ?)

拔了角的鹿 2024-09-26 02:02:14

从查尔斯那里偷一点:

AND Substring(dbo.AssnCtrl.Name, 1, 1) Like
  CASE WHEN @FL = '0' THEN '[0-9]'
    ELSE @FL            
  END

To steal a bit from Charles:

AND Substring(dbo.AssnCtrl.Name, 1, 1) Like
  CASE WHEN @FL = '0' THEN '[0-9]'
    ELSE @FL            
  END
痕至 2024-09-26 02:02:14

你试过

AND (
        isnumeric(substring(dbo.AssnCtrl.Name, 1, 1)) = 1
    or
        SUBSTRING(dbo.AssnCtrl.Name, 1, 1) = @FL    )

这对我有用吗:

select * from casefile where
isnumeric(substring(pmin,1,1)) = 1
or 
substring(pmin,1,1) = 'a'

Have you tried

AND (
        isnumeric(substring(dbo.AssnCtrl.Name, 1, 1)) = 1
    or
        SUBSTRING(dbo.AssnCtrl.Name, 1, 1) = @FL    )

this works for me:

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