What is the best way to handle null middle initials in a SQL query?

  • Thread starter Thread starter WWGD
  • Start date Start date
AI Thread Summary
To construct a full name from the Emp table while handling potential null values in the middle name initial (EmpMidI), the use of the COALESCE function or conditional statements is recommended. A suggested SQL query format is: SELECT EmpFirst + IIF(ISNULL(EmpMidI), '', ' ' + EmpMidI) + ' ' + Emplast AS [fullname]. It's important to ensure consistent naming conventions across the columns, as discrepancies can lead to confusion and maintenance challenges. Additionally, the use of the SET CONCAT_NULL_YIELDS_NULL OFF option is discouraged due to its deprecation in future SQL Server versions and potential issues in multiuser environments. Overall, handling nulls effectively in SQL queries is crucial for accurate data representation.
WWGD
Science Advisor
Homework Helper
Messages
7,679
Reaction score
12,376
Hi all,
I have a table Emp ( EmpFName, EmpMidI, Emplast) ,

where EmpMidI is the middle name initial, which may be null. I want to get the full name in a single
record, which I would do, if there were no nulls, as :

Select EmpFirst + ' ' + EmpMidI + ' ' + Emplast AS [fullname]

How do I rewrite this query to allow for null middle initials?
Thanks.
 
Technology news on Phys.org
WWGD said:
Hi all,
I have a table Emp ( EmpFName, EmpMidI, Emplast) ,

where EmpMidI is the middle name initial, which may be null. I want to get the full name in a single
record, which I would do, if there were no nulls, as :

Select EmpFirst + ' ' + EmpMidI + ' ' + Emplast AS [fullname]

How do I rewrite this query to allow for null middle initials?
Thanks.
You probably want to use something like the coalesce function to change a null value to something else.
http://www.w3schools.com/sql/sql_isnull.asp
 
  • Like
Likes WWGD
You could just write an IF ELSE statement like you would in any programming language.

IF EmpMidI IS NOT NULL
BEGIN
--include EmpMidl
END
ELSE
BEGIN
--omit EmpMidl
END
 
  • Like
Likes Silicon Waffle
Depending on your SQL dialect, something like
Code:
SELECT EmpFirst + IIF(ISNULL(EmpMidI), '', ' ' + EmpMidI) + ' ' + Emplast AS [fullname]
but note:
  1. You have called the first column EmpFName in the table definition and EmpFirst in the query
    [*]This is probably because you have managed to use 3 different naming conventions for your 3 columns - this should get an award for unmaintainable code!* Pick one and stick to it: I would probably choose FirstName, MiddleInitials, LastName (note that there may be more than one middle initial and prefixing column names with the table name is not in general a good habit).
    [*]I can't think of a situation where you would want to do this in SQL - this kind of manipulation is best left to the client application.


* I know this is only an example, but you need to work at it until these things become instinctive.
 
  • Like
Likes Silicon Waffle
How about this ?

SET CONCAT_NULL_YIELDS_NULL OFF
SELECT EmpFirst + ' ' + EmpMidI + ' ' + Emplast AS [fullname]
 
Silicon Waffle said:
How about this ?

SET CONCAT_NULL_YIELDS_NULL OFF
SELECT EmpFirst + ' ' + EmpMidI + ' ' + Emplast AS [fullname]
  1. MS specific.
  2. Even MS have deprecated it ("In a future version of SQL Server CONCAT_NULL_YIELDS_NULL will always be ON and any applications that explicitly set the option to OFF will generate an error").
  3. Potentially disastrous in a multiuser environment, or single user if you forget to turn it back on again.
 
  • Like
Likes Silicon Waffle
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top