Reputation: 195
Today I have encountered an odd issue with ISNULL function in SQL
I have table contains the customer information I have to display a full name concatenating First, Middle Initial and Last Name, In the table I know Middle Initial is a nullable column.
So I used ISNULL function in SQL to return the Middle Initial if it is not null, which works fine but I used the ISNULL function with a expression as below which I ideally should not work in case if the MI is null, but interestingly it worked.
SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI + '.', '') As MiddleInitial
FROM CS_tblMaster WHERE CustomerNo = 2627240
Above SOL Query should return me MiddleInitial as "." when MiddleInitial is null, but it did return as empty string.
So I wrote another query as below.
SELECT (MI + '.') AS MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240
which again given as NULL
Somehow when you concatenate a string with NULL value it returns null. I would like to understand of this implementation.
Can someone help
Upvotes: 4
Views: 4168
Reputation: 1
What about NULLIF:
SELECT FirstName + ' ' + LastName + ' '+ ISNULL((NULLIF(MI + '.', '.'), '') As MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240
Upvotes: 0
Reputation: 4048
– Original statement
SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI + '.', '') As MiddleInitial
FROM CS_tblMaster WHERE CustomerNo = 2627240
– My original answer (edited)
SELECT FirstName + ' ' + LastName + ' '+ (ISNULL(MI, '') + '.') As MiddleInitial
FROM CS_tblMaster WHERE CustomerNo = 2627240
In this case, SQL would first check to see if MI is Null and uses MI if Not Null or uses an empty string if it is. Then it concatenates that result, which is now never Null, with the period.
– Final answer
SELECT
FirstName + ' ' + LastName + ' '
+ CASE WHEN MI IS NULL THEN '' ELSE MI + '.' END As MiddleInitial
FROM CS_tblMaster WHERE CustomerNo = 2627240
@Satish, not sure if you feel you have answer yet since you haven't selected one, and I apologize if my answer was short and fast. Seeing all the responses made me realize I hadn't thought much about your question when I first saw it.
To answer “I would like to understand of this implementation”, Nulls are a completely special value in SQL. Not an empty string, not spaces, not zeros. They mean in a more literal sense “nothing”. You can check for them, can see if something is null. But you can't “do” things with Nulls. So 57 + Null = Null. 'Mary' + Null = Null. ((12 *37) +568) / Null = Null. The Max() of 'Albert', 'Mary', Null, and 'Zeke' is Null. This article http://en.wikipedia.org/wiki/Null_(SQL) may help, with a decent description in the section on Null Propagation.
The Isnull function is not so much a test for Null, but a way to handle it. So to test if something is Null you would use ColumnName Is Null or ColumnName Is Not Null in your Select. What Isnull(MI,'') says is: I want a value, if the value of MI it not null, then I want MI, otherwise I want an empty string.
Going on, I'm not sure I initially understood what you were actually trying to do. If you were trying to get a period when the middle initial was null, then my original answer and most of the others would work for you. But I think you may be trying to say: If I have a middle initial, then I want the middle initial followed by a period. If I don't have middle initial then I want nothing: 'Alberto C. Santaballa' or 'Alberto Santaballa', never 'Alberto . Santaballa”. If that is the case then use the final statement in the edited answer.
@Zec, thanks for the edit. The typo was another product of too-fast typing! :-/
Upvotes: 1
Reputation: 610
brothers, i guess u are misunderstanding isnull function
ISNULL([Columns To Check],[Replaced String])
SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI + '.', '') As MiddleInitial
FROM CS_tblMaster WHERE CustomerNo = 2627240
ISNULL function refer to is the column you want to check is null, then replace it with some others string
in your query, u are trying to replace ur MI+'.' columns to Empty string, hopefully my explanation can help u up=)
Upvotes: 0
Reputation: 2657
If you want to put a period if the MI is null use this: (You just have your '.'
in the wrong spot)
SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI, '.') As MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240
Upvotes: 3
Reputation: 4048
If you concatenate to a null you get a null. Take the period outside. Isnull(MI, '') + '.'
Upvotes: 0
Reputation: 3834
Will something like this work for you...
SELECT FirstName + ' ' + LastName + ' '+ CASE WHEN MI is null then '.' else MI END As MiddleInitial
FROM CS_tblMaster WHERE CustomerNo = 2627240
END
Upvotes: 0