Sql 如果值为null,则前进选择下一列

Sql 如果值为null,则前进选择下一列,sql,sql-server,Sql,Sql Server,我只需要列所在的表中的一个血压值 B/p |坐|卧|站 不寻常的是,有一般血压,然后是每个姿势的血压。我基本上只需要这些值中的一个,重要性从左到右。如果血压为零,则选择坐位值;如果坐位值为零,则选择卧位值;如果卧位值为零,则选择站立值 我需要像这样的东西 SELECT case when bloodpressure is null or bloodpressure = '' then case when sitting is null or sitting = ''

我只需要列所在的表中的一个血压值

B/p |坐|卧|站

不寻常的是,有一般血压,然后是每个姿势的血压。我基本上只需要这些值中的一个,重要性从左到右。如果血压为零,则选择坐位值;如果坐位值为零,则选择卧位值;如果卧位值为零,则选择站立值

我需要像这样的东西

SELECT
    case when bloodpressure is null or bloodpressure = ''
        then case when sitting is null or sitting = ''
            then case when lying is null or lying = ''
                then case when standing is null or standing = ''
                    standing
                end
                lying
            end
            sitting
        end
    bloodpressure
    end
    as bloodpressure
试试这个

SELECT CASE
         WHEN bloodpressure IS NOT NULL
               AND bloodpressure <> '' THEN bloodpressure
         WHEN sitting IS NOT NULL
               AND sitting <> '' THEN sitting
         WHEN lying IS NOT NULL
               AND lying <> '' THEN lying
         WHEN standing IS NOT NULL
               AND standing <> '' THEN standing
       END AS bloodpressure 
选择案例
当血压不为零时
血压“,然后是血压
什么时候坐不空
坐着,然后坐着
当说谎不是空的时候
然后说谎,然后说谎
站立时不为空
站着,然后站着
以血压结束
不为空,并且站着的“”
可以替换为
NULL如果(站着的“”)不为空

这看起来像:

SELECT 
    CASE 
        WHEN ISNULL(bloodpressure,'') <> '' THEN bloodpressure
        WHEN ISNULL(sitting,'') <> '' THEN sitting
        WHEN ISNULL(lying,'') <> '' THEN lying
        WHEN ISNULL(standing,'') <> '' THEN standing
    ELSE 
        '0' END AS bloodpressure
选择
案例
当ISNULL(血压“”)时,则血压
当ISNULL(SITING,“”)时,则为SITING
当ISNULL(说谎“”)时,则说谎
当ISNULL(站着“)”时,则站着
其他的
“0”结束为血压
我还将
ELSE
条件添加为
0
,因为如果所有字段都为空,则必须接收0。如果不需要,您可以将其删除


如果所有enpty值均为空,则可以使用

如果值为空或有效,则可以尝试:

SELECT ISNULL(bloodpressure, ISNULL(sitting, ISNULL(lying, standing))) AS bloodpressure
或者使用
合并
(正如@Bogdan-Bogdanov所建议的)


如果出现以下情况,也可以使用coalesce+Null:

select coalesce (
  nullif(bloodpressure,''), 
  nullif(sitting,''), 
  nullif(lying,''), 
  standing)

另外,您不应该在列中存储空字符串,这只是糟糕的数据库设计。

@Lashane-Yep,updateEdit也必须不是
空字符串
,如果所有元素都是
NULL
(不是
空字符串
),您可以使用
合并
select coalesce (
  nullif(bloodpressure,''), 
  nullif(sitting,''), 
  nullif(lying,''), 
  standing)