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 8741453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:19:06+00:00 2026-06-13T11:19:06+00:00

I know I can do this in my programming language, but I want to

  • 0

I know I can do this in my programming language, but I want to try to do it inside the SQL so that I dont have to disturb any frontend code.

I have a table with three name fields – name1, name2, name3.

There might be values in any or all of these fields in random.

I want a query which will return three fields such that,

It will check name1,name2,name3 – which ever first field has value goes into first_name,
which ever second field has value goes into second_name, and then third_field.

I can easily write a CASE-END statement for populating first_name, but when I get to writing the logic for second_name its getting more complicated.

The SQL:

SELECT 
  CASE
    WHEN NAME1 IS NOT NULL
    THEN NAME1
    ELSE
      CASE
        WHEN NAME2 IS NOT NULL
        THEN NAME2
        ELSE
          CASE
            WHEN NAME3 IS NOT NULL
            THEN NAME1
            ELSE ''
          END
      END
  END FIRST_NAME
  FROM TABLE_NAME

Now, how do I handle second_name and third_name? Or is their an easier way?

Any help is appreciated..

  • 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-06-13T11:19:07+00:00Added an answer on June 13, 2026 at 11:19 am

    You could potentially do something like

    SELECT coalesce( name1, name2, name3 ) first_name,
           (case when name1 is not null 
                 then nvl( name2, name3 )
                 when name2 is not null
                 then name3
                 else null
             end) second_name,
           (case when name1 is not null and
                      name2 is not null
                 then name3
                 else null
             end) third_name
      FROM your_table
    

    However the desire to do this seems to indicate that you have a data model problem that needs to be addressed. If you have three columns that apparently all store the same data, the table is not normalized correctly. It would be a better data model (and an easier query) if you stored the names in a separate table that had a 1:n relationship with the existing table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In any programming language, I can trace any function and know which function is
I know this can seem a weird question but for me it would be
I know this can be done and i have seen it done using some
I know you can do this <%= Request.Form[0] %> But how do you do
I know you can do this with virtual attributes, but what if the column
I know you can do this.GetType().FullName To get My.Current.Class But what can I call
I have a small programming language I've been designing, and I want a relatively
Ok, This question is not exactly a programming question but this is what can
I know you can do this to get vertical text in a tab header:
I know I can use this way to find the most frequent element in

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.