Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determine if any values are null, if true then false, else true

I currently have a select statement that checks several columns to see if they have data. if any of them are null then i want a bit set to false. if none of them are null then i want a bit set to true. here's what i currently have:

select
cast(
        case when ChangeOrderNumber is null then 0 else 1 end *
        case when ClientName is null then 0 else 1 end *
        case when QuoteNumber  is null then 0 else 1 end *
        case when ClientNumber is null then 0 else 1 end *
        case when ServiceLine is null then 0 else 1 end *
        case when ServiceLineCode is null then 0 else 1 end *
        case when GroupLeader is null then 0 else 1 end *
        case when CreatedBy is null then 0 else 1 end *
        case when PTWCompletionDate is null then 0 else 1 end *
        case when BudgetedHours is null then 0 else 1 end *
        case when BudgetDollars is null then 0 else 1 end *
        case when InternalDeadlineDate is null then 0 else 1 end *
        case when ProjectDescription is null then 0 else 1 end *
        case when Sales is null then 0 else 1 end *
        case when Coop is null then 0 else 1 end *
        case when PassThrough is null then 0 else 1 end *
        case when POStatus is null then 0 else 1 end *
        case when PONumber is null then 0 else 1 end as bit
    ) 
    as Flag
from t

now, that code works, but it's a bit lengthy, i was wondering if anyone knew of a better way to do this. please note that there are several data types being checked.

further details: this code is in a view that is being looked at in an application for processing change orders. before a change order can be processed it must meet some data quality checks. this view shows if any of the required data is null.

like image 339
DForck42 Avatar asked May 24 '11 20:05

DForck42


People also ask

Can NULL values be compared State True or false?

Comparisons to null The SQL null value basically means “could be anything”. It is therefore impossible to tell whether a comparison to null is true or false.

What are the three-valued logic values for the OR clause with NULL and not?

Therefore, a logical expression may return one of three-valued logic: TRUE , FALSE , and UNKNOWN . The NULL does not equal anything, even itself. It means that NULL is not equal to NULL because each NULL could be different.

What is the use of IsNull () function?

Returns a Boolean value that indicates whether an expression contains no valid data (Null). The required expressionargument is a Variant containing a numeric expression or string expression.


2 Answers

Please use IIF() (need to be sql server 2012 or later) I really recommend:

IIF(column1 is null, '0', '1')
like image 112
Xin Avatar answered Oct 11 '22 04:10

Xin


I think I might go with this solution unless someone comes up with a better one, inspired by @Alireza:

cast(
        case when (ChangeOrderNumber is null  or
        a.ClientName is null  or
        a.QuoteNumber  is null  or
        ClientNumber is null  or
        ServiceLine is null  or
        ServiceLineCode is null  or
        GroupLeader is null  or
        CreatedBy is null  or
        PTWCompletionDate is null  or
        BudgetedHours is null  or
        BudgetDollars is null  or
        InternalDeadlineDate is null  or
        ProjectDescription is null  or
        Sales is null  or
        Coop is null  or
        PassThrough is null  or
        POStatus is null  or
        PONumber is null) then 'false' else 'true'
        end as bit) as Flag
like image 29
DForck42 Avatar answered Oct 11 '22 05:10

DForck42