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

  • Thread starter Thread starter WWGD
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around handling null middle initials in SQL queries, specifically how to construct a full name from a table containing first names, middle initials, and last names. Participants explore various methods to ensure that null values do not disrupt the concatenation of names.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using the COALESCE function to replace null values with an alternative during concatenation.
  • Another proposes using an IF ELSE statement to conditionally include or omit the middle initial based on its null status.
  • A different approach is mentioned that utilizes the IIF function along with ISNULL to manage null middle initials, while also critiquing the inconsistency in column naming conventions in the provided example.
  • One participant mentions setting CONCAT_NULL_YIELDS_NULL to OFF as a method to handle nulls, but notes that this is specific to Microsoft SQL and may lead to issues in future versions.
  • Another participant echoes the previous point about CONCAT_NULL_YIELDS_NULL, warning of potential problems in multiuser environments and the risk of forgetting to revert the setting.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to handle null middle initials in SQL queries, with no consensus reached on the best approach.

Contextual Notes

There are concerns regarding the use of different naming conventions for columns, which may affect code maintainability. Additionally, the implications of using specific SQL settings like CONCAT_NULL_YIELDS_NULL are noted, particularly regarding future compatibility.

WWGD
Science Advisor
Homework Helper
Messages
7,802
Reaction score
13,106
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: Silicon Waffle

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 51 ·
2
Replies
51
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
463