利用子字符串和 isnumeric 的 T-SQL Case 语句
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接使用 Like 运算符?
当他们选择“任意数字”选项时,将 @FL 作为“[0-9]”传递
(我假设您在此名称列上有索引?)
Why not just use Like operator ?
When they select the Any Number option, pass in @FL as '[0-9]'
(I assume you have an index on this name column ?)
从查尔斯那里偷一点:
To steal a bit from Charles:
你试过
这对我有用吗:
Have you tried
this works for me: