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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:26:37+00:00 2026-06-17T03:26:37+00:00

Disclaimer: I am still learning SQL so I apologize if my question comes off

  • 0

Disclaimer: I am still learning SQL so I apologize if my question comes off as amateur-ish or is otherwise a very simple answer. I have no formal training. I am teaching myself. Thanks.

A particular query was created to update the EMAIL column with data in the EMAIL2 column should the EMAIL column be blank. This query goes on to grab data from the EMAIL3-6 columns should any prior ones also be blank in an attempt to populate the EMAIL column. It currently sits as follows:

update Parents
set email = email2
where email = ' ' OR email is null
go

update Parents
set email = email3
where email = ' ' OR email is null
go

update Parents
set email = email6
where email = ' ' OR email is null
go

(and so on)

Is there a more simple way, using some sort of IF…THEN type conditions to check for blank or null in the EMAIL column and populate it based on data in the secondary column(s)? These columns would also need to be checked for blank or null values and skipped if blank or null is true.

I appreciate any advice that can be given. The above query works it just doesn’t feel like the best way to go about doing it.

  • 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-17T03:26:39+00:00Added an answer on June 17, 2026 at 3:26 am

    A handy function you will want to become aquainted with is NULLIF. It allows you to simplify your logic in cases where you might like to treat a value like a NULL. For example, if an application was putting a sentinel value of ‘NA’ in a NULLable column column1, NULLIF(column1, 'NA') would return the NULL value for all the NULLs and all the NAs. In your case, we’d use this trick to convert empty strings into NULLs.

    The other thing we’ll do is trim all the empty strings down so our NULLIF only needs to check for the scenario of ” (instead of ”, ‘ ‘, ‘ ‘, ad nauseum). TSQL only provides LTRIM and RTRIM and I have a habit of using RTRIM although trimming an empty string from either direction will result in our desired state. NULLIF(RTRIM(column1),'')

    Using that expression, we will now have the right thing to plug into the COALESCE function. Thus

    UPDATE
        P
    SET
        email = COALESCE(NULLIF(RTRIM(P.email2), ''), NULLIF(RTRIM(P.email3), ''), NULLIF(RTRIM(P.email4), ''))
    FROM
        dbo.Parents P
    WHERE
        -- This will force a table scan which can be a bad thing for performance
        -- Generally, one should avoid wrapping functions around a column and instead
        -- apply them to the thing being tested
        NULLIF(RTRIM(P.email), '') IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I am still learning SQL so I apologize if my question comes off
Disclaimer: I am still learning SQL so I apologize if my question comes off
The Disclaimer First of all, I know this question (or close variations) have been
Again disclaimer disclaimer still learning C# and OOP generally so I hope you'll be
Disclaimer: I don't have much SQL experience, and I don't know what version of
Disclaimer : My apologies for all the text below (for a single simple question),
First of all, a disclaimer: I have experience in other languages, but am still
Disclaimer: my SQL skills are basic, to say the least. Let's say I have
Disclaimer: i still suck at writing scripts please be gentle if this is a
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial

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.