开发者

Show field dependent on whether other field contains data

开发者 https://www.devze.com 2023-04-12 15:36 出处:网络
Is it possible in an SQL query to only show a field if another field has data?For example, if Field1 <> \'\', 开发者_开发知识库then show the value in Field2 else don\'t show the value?It can be don

Is it possible in an SQL query to only show a field if another field has data? For example, if Field1 <> '', 开发者_开发知识库then show the value in Field2 else don't show the value?


It can be done using a case statement. (At least in SQL Server)

select case when Field1 <> '' 
            then Field2  
       end as Field2
from YourTable


Sure (this works in Oracle and SQLite):

select 
  field1,
  (case 
     when field1 is null then null
     else field2
   end) field2_wrapped
from my_table


if 'has no data' means the empty string (''), you need to use this statement:

SELECT Filed2 FROM Table1 WHERE Filed1<>''

If 'no data' means NULL value, you need use

SELECT Filed2 FROM Table1 WHERE NOT (Filed2 IS NULL)


Take a look at Standard SQL functions COALESCE() and NULLIF():

COALESCE(NULLIF(Field1, ''), Field2)
0

精彩评论

暂无评论...
验证码 换一张
取 消