Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234000
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:20:29+00:00 2026-05-17T17:20:29+00:00

I have a need to build a string from Last Name, First Name, Middle

  • 0

I have a need to build a string from Last Name, First Name, Middle Initial according to the following rules:

  1. If the Last Name is unique, just
    return the Last Name
  2. If the Last
    Name isn’t unique, but the first
    letter of the First Name is unique,
    return Last Name + first letter of
    First Name
  3. If the Last Name and
    first letter of the First Name are
    not unique, return the Last Name +
    first letter of First Name + Middle
    Initial.

For example, the table might be:

   MDC   MDLast      MDFirst     MDInit
    3    Jones       Fred         A    
    21   Smith       Sam          D  
    32   Brown       Tom          E  
    42   Brown       Ted          A  
    55   Smith       Al           D  

The query should return:

MDC  MDFormattedName    
3    Jones  
21   Smith S  
32   Brown TE  
42   Brown TA  
55   Smith A  

I’ve written up a query that almost works, but it is using several nested queries, and will still need several more to (possibly) make a workable solution, and is so inefficient. I’m sure there is a ‘proper’ way to implement this (for SQL Server 2005, BTW).

This is what I’ve got so far. It doesn’t work, due to the aggregations I lose the IDs can can’t do the final join to get ID/Name pairs.

select 
    CASE
        WHEN CountLastFirst > 1 THEN
            CASE WHEN MDInit IS NOT NULL  THEN MDLastFirst + LEFT(MDInit,1) ELSE MDLastFirst END
        WHEN CountLastFirst = 1 AND CountLast > 1 THEN MDLastFirst
        ELSE MDLast
    END as MDName

FROM

(

select x.MDLast, CountLast, MDLastFirst, CountLastFirst FROM
(
select   MDLast,Count(MDLast) as CountLast FROM
MDList
GROUP BY MDLast) as x

INNER JOIN
(select MDLast,   MDLastFirst,Count(MDLastFirst) as CountLastFirst FROM
(
select MDLast,
MDLast + ' ' + LEFT(MDFirst,1) as MDLastFirst
From MDList
) as a
GROUP BY MDLastFirst, MDLast) as y ON x.MDLast = y.MDLast
) as z
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:20:30+00:00Added an answer on May 17, 2026 at 5:20 pm

    Assuming a table name of MDCTable, this should work:

    SELECT MDCTable.MDC,
        CASE MDCCount.NameCount 
        WHEN 1 
        THEN MDCTable.MDLast 
        ELSE    
            CASE MDFormat1Count
            WHEN 1 
            THEN MDFormat1.MDFormat1Name
            ELSE MDCTable.MDLast + ' ' + upper(left(MDCTable.MDFirst, 1)) + 
                      MDCTable.MDInit
            END
        END AS MDFormattedName
    FROM MDCTable 
    INNER JOIN 
    (
        SELECT COUNT(MDLast) as NameCount, MDLast
        FROM MDCTable
        GROUP BY MDLast
    ) MDCCount ON MDCCount.MDLast = MDCTable.MDLast
    INNER JOIN (
        SELECT COUNT(MDLast + left(MDFirst, 1)) as MDFormat1Count, MDLast + ' ' + 
             left(MDFirst, 1) AS MDFormat1Name
        FROM MDCTable
        GROUP BY MDLast + ' ' + left(MDFirst, 1) 
    ) MDFormat1 ON MDCTable.MDLast + ' ' + left(MDCTable.MDFirst, 1) = 
         MDFormat1.MDFormat1Name
    ORDER BY MDCTable.MDC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Monthly Status database view I need to build a report based
I have this large C++ project that I need to build on a platform
We have inherited an ant build file but now need to deploy to both
I need to learn ADO.NET to build applications based on MS Office. I have
I have need to select a number of 'master' rows from a table, also
I have a need to close a parent form from within child form from
We have need for a rating system in a project we are working on,
I have the need to read the Thunderbird address book on the fly. It
I have a need to display a UITableView containing a user's account credentials. For
I have a need to create a transactional process using an external API that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.