Sixthsense
Sixthsense

Reputation: 1975

SQL Error (4145): An expression of non-boolean type specified in a context where condition is expected, near '('

Using the below statement to check whether a field is null and if it is null then I need to show "Not Yet Approved" message. Otherwise I would like to get a concatenated result of some fields which is mentioned below.

select 
    iif(isnull(AppByENo1,'true'),'Not Yet Approved',AppByENo1+AppByDesg1+AppByDate1) as result
from myDB 
    where (E_No = '25')

But getting the above mentioned error while trying to run the sql query.

Please advice where I am making mistake and how to tackle this issue.

Upvotes: 0

Views: 7157

Answers (3)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239664

Since concatenating null values yields null (providing you're leaving appropiate settings turned on, which you should), you can just use a simple coalesce to replace the null if necessary:

select 
    COALESCE(AppByENo1+AppByDesg1+AppByDate1,'Not Yet Approved') as result
from myDB 
    where (E_No = '25')

If you insist on the iif approach, I'd favour:

iif(AppByENo1 is null,'Not Yet Approved',AppByENo1+AppByDesg1+AppByDate1)

E.g. an actual null test, rather than trying to identify a sentinel string that cannot appear naturally and performing a string comparison after isnull.

Upvotes: 1

Mansoor
Mansoor

Reputation: 4192

Use CASE Statement in SELECT clause instead of IFF :

  SELECT CASE WHEN ISNULL(AppByENo1,'true') = 'true' THEN 'Not Yet Approved'
     ELSE AppByENo1+AppByDesg1+AppByDate1 END as result
  FROM myDB WHERE E_No = '25'

Upvotes: 1

Abdullah Dibas
Abdullah Dibas

Reputation: 1507

try this:

select 
iif(isnull(AppByENo1,'true') = 'true','Not Yet Approved',AppByENo1+AppByDesg1+AppByDate1) as result
from myDB 
where (E_No = '25')

Upvotes: 3

Related Questions