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,700
Reaction score
12,698
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
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top