Question

Trying to run this query in LINQPad 4:

SELECT item_group_id as AccountID, IIF(ISNULL(t_item_group.description),'[blank]',t_item_group.description) AS Name 
FROM t_item_group 
WHERE active = TRUE

I get, "the isnull function requires 2 argument(s)."

I've tried moving the parens around, changing the "[blank]" to "[blank]" and "[blank]" , but none of it helps...

The queries (I have two similar ones (with IIF(ISNULL)) that LINQPad won't run for this reason, yet they run in actuality (in my Web API app) fine; so, LINQPad is more "picky" than it needs to be, perhaps, but what is it expecting, SQL syntax-wise?

Was it helpful?

Solution

ISNULL is already like a 'if' type statement.

You can just replace

IIF(ISNULL(t_item_group.description),'[blank]',t_item_group.description)

with

ISNULL(t_item_group.description, '[blank]')

The ISNULL uses the first parameter (the 'description'), unless that value is null in which case it will use the second parameter.


As an aside, one of the reasons I don't care for ISNULL is that it is poorly named. You'd assume that given its name it will return a bit - true if the parameter is null, false if not null - which you could use in an 'if' statement like you attempted. But that's not how it works.

The alternative is to use COALESCE. It provides much the same functionality, but the naming makes sense.

co·a·lesce ˌkōəˈles verb 1. come together and form one mass or whole.

To COALESCE two parameters is to force them into one non-nullable result. And the function is actually more powerful, as you can provide multiple parameters - COALESCE(i.description, i.name, '[blank]') is perfectly valid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top