3

Is there a function that is similar to COALESCE but for values that are not NULL?

I need to replace a return value of a scalar-valued-function with another value if it returns the string literal N/A. If it would return NULL i could use:

SELECT COALESCE(dbo.GetFirstSsnInFromSsnOut(RMA.IMEI), RMA.IMEI) AS [IMEI to credit]
     , OtherColumns
FROM dbo.TableName

But since it returns N/A this doesn't work. I could also use:

SELECT CASE WHEN dbo.GetFirstSsnInFromSsnOut(RMA.IMEI)='N/A' 
         THEN RMA.IMEI 
         ELSE dbo.GetFirstSsnInFromSsnOut(RMA.IMEI)
       END AS [IMEI To Credit]         
    , OtherColumns
FROM dbo.TableName

But that would be inefficient since it needs to execute the function twice per record.

Note that this query is in a table-valued-function.

3
  • But can't you just modify the function GetFirstSsnInFromSsnOut itself to return NULL instead of N/A ? Commented Nov 15, 2013 at 10:27
  • @StephenByrne: That would cause other problems/work. So i've asked first if there is a way to solve this without the "breaking change". The method is already used in some excel addins that i would have to modify then. Commented Nov 15, 2013 at 10:28
  • What about creating another function that wraps GetFirstSsnInFromSsnOut and changes N/A to NULL? Doing that, you could use COALESCE and still have only one call to GetFirstSsnInFromSsnOut Commented Nov 15, 2013 at 10:31

3 Answers 3

10

Perhaps there you can use NULLIF function, for example:

SELECT ISNULL(NULLIF(dbo.GetFirstSsnInFromSsnOut(RMA.IMEI), 'N/A'), RMA.IMEI) AS [IMEI to credit]
     , OtherColumns
FROM dbo.TableName;
Sign up to request clarification or add additional context in comments.

12 Comments

@TimSchmelter - This may well execute the function twice anyway. ISNULL is generally better than COALESCE for this.
@MartinSmith: Really, why? Can i prevent that in some way?
See the connect item linked above. If the plan shows the function referenced twice because of how COALESCE gets expanded to CASE then you can replace COALESCE with ISNULL
I changed COALESCE to ISNULL (I personally use ISNULL in most cases)
Seems that COALESCE always gets expanded to CASE. Was an eye opener for me! msdn.microsoft.com/en-us/library/ms190349.aspx
|
5

If you can't change GetFirstSsnInFromSsnOut to return Null-Values then try this:

SELECT COALESCE(dbo.GetFirstSsnInFromSsnOut(NULLIF(RMA.IMEI, 'N/A')), RMA.IMEI) AS [IMEI to credit] , OtherColumns FROM dbo.TableName

1 Comment

Thanks to all, you were the fastest, but Andrey's answer contains the the function and link i've searched. I have upvoted all three anyway.
5

The function nullif returns null if its arguments are equal, and its first argument otherwise. Combine with coalesce for fun and profit:

select
    coalesce(
        nullif(dbo.GetFirstSsnInFromSsnOut(RMA.IMEI), 'N/A'),
        RMA.IMEI
    ) as [IMEI To Credit]
    , OtherColumns
from dbo.TableName

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.