sql中可以有多个case then语句吗

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

我正在尝试一些需要案例中案例的事情,我只是想确定我们是否可以使用多个案例?我正在 sql teradata 上运行它 我尝试使用的代码如下

AND(
    case when CHARACTER_LENGTH(drug.n)=0 then 0 

     when CHARACTER_LENGTH(drug.n)=1 then
     (case when substring(drug.n from  1,1) in            (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end)


     when CHARACTER_LENGTH(drug.n)=2 then 
     (case when substring(drug.n from  1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
     case when substring(drug.n from  2,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end ) 


     when CHARACTER_LENGTH(drug.n)=3 then 
     (case when substring(drug.n from  1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
     case when substring(drug.n from  2,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
     case when substring(drug.n from  3,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end )=1

如果有人有更好的想法,您可以告诉我。我无法使用 isnumeric 函数。

I am trying for something which requires case within case I just wanted to make sure if we can use multiple case then?I am running this on sql teradata
The code I am trying to use is as below

AND(
    case when CHARACTER_LENGTH(drug.n)=0 then 0 

     when CHARACTER_LENGTH(drug.n)=1 then
     (case when substring(drug.n from  1,1) in            (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end)


     when CHARACTER_LENGTH(drug.n)=2 then 
     (case when substring(drug.n from  1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
     case when substring(drug.n from  2,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end ) 


     when CHARACTER_LENGTH(drug.n)=3 then 
     (case when substring(drug.n from  1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
     case when substring(drug.n from  2,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
     case when substring(drug.n from  3,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end )=1

If somebody has better idea you can let me know. I cannot use isnumeric function.

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

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

发布评论

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

评论(2

意中人 2024-12-17 08:08:43

是的,您可以使用嵌套的 CASE 语句。 Teradata 中没有问题

Yes you can use nested CASE statements. No problems with that in Teradata

倥絔 2024-12-17 08:08:43

好的 -
要确定任意长度的字符串是否仅包含数字字符(或不包含),可以使用递归 CTE。

请注意,我不知道您的 RDBMS 是否实际上支持递归 CTE,但这是一个潜在的解决方案。另外,我不确定对性能的影响 - 但是,它确实消除了多个 CASE 效果(无论如何,为什么它不是一个实际的数字字段?)。

所以...对于看起来像这样的表:

id   ch
================
1   1234567890
2   asdg      

此语句返回仅包含数字字符(任意长度)的所有行:

WITH splitstring (id, chard, start, orig) as (
                  SELECT id, SUBSTRING(ch, 1, 1), 1, ch
                  FROM chartable
                  UNION ALL
                  SELECT id, SUBSTRING(orig, start + 1, 1), start + 1, orig
                  FROM splitstring
                  WHERE LENGTH(orig) > start)
SELECT * 
FROM chartest as a
WHERE NOT EXISTS (SELECT '1'
                  FROM splitstring as b
                  WHERE a.id = b.id
                  AND chard NOT BETWEEN '0' AND '9')

如果没有一些更大的上下文,则很难准确地知道您的内容正在努力实现。但是,这应该适合您的需求。

(顺便说一句,iSeries 的 DB2 似乎也不支持正则表达式......)

Okay -
To determine whether an arbitrary length string contains only numeric characters (or does not), you can use a recurive CTE.

Please note that I don't know whether or not your RDBMS actually supports recursive CTEs, but this is a potential solution. Also, I'm not sure of the performance implications - however, it does remove the multiple CASE effect (And why isn't that an actual numeric field, anyways?).

So... For a table that looks like this:

id   ch
================
1   1234567890
2   asdg      

This statement returns all rows that contain only numeric characters (of any length):

WITH splitstring (id, chard, start, orig) as (
                  SELECT id, SUBSTRING(ch, 1, 1), 1, ch
                  FROM chartable
                  UNION ALL
                  SELECT id, SUBSTRING(orig, start + 1, 1), start + 1, orig
                  FROM splitstring
                  WHERE LENGTH(orig) > start)
SELECT * 
FROM chartest as a
WHERE NOT EXISTS (SELECT '1'
                  FROM splitstring as b
                  WHERE a.id = b.id
                  AND chard NOT BETWEEN '0' AND '9')

Without some of the larger context it's somewhat difficult to know exactly what you're trying to accomplish. However, this should be adaptable for your needs.

(As a side note, DB2 for the iSeries doesn't seem to support regex either...)

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