SQL Server function to check for null and spaces

Hello Experts,
I am using ISNULL function to get the column to populate from two columns which do not have a Null.

Example ISNULL(ColumnA,ColumnB) as ABC  Polulates ColumnB Value in the Column 'ABC' when columnA is null. Now we have some rows in columnA that is empty space like ' ' instead of a Null.  What changes do I need to make to my SQL so it will populate ColumnB value in ABC when the ColumnA is an empty space instead of a null.

Issue is below:

ColumnA    ColumnB     ABC
  NULL           Hello         Hello
  '  '                 Hello          '   '

Expected Solution

ColumnA    ColumnB     ABC
  NULL           Hello         Hello
  '  '                 Hello         Hello

Thanks in advance
Milind AgarwalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aranaCommented:
not sure if trimmed white space will return null but you can try ISNULL(rtrim(ColumnA),ColumnB) as ABC

or

 ISNULL(NULLIF(ColumnA, ' '),ColumnB) as ABC

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott FellDeveloper & EE ModeratorCommented:
https://msdn.microsoft.com/en-us/library/ms190349.aspx

I think you can do this.

SELECT ColumnA , ColumnB , 
     CASE 
          WHEN isnull(ColumnA , '') = '' THEN ColumnB 
          WHEN ColumnA = '' THEN ColumnB 
     END AS ABC
FROM MyTable

Open in new window

Scott FellDeveloper & EE ModeratorCommented:
You may want to add an ELSE so the TEST column is not null

SELECT ColumnA , ColumnB , 
     CASE 
          WHEN isnull(ColumnA , '') = '' THEN ColumnB 
          WHEN ColumnA = '' THEN ColumnB 
          ELSE 'N/A'
     END AS ABC
FROM MyTable

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.