Link to home
Start Free TrialLog in
Avatar of Milind Agarwal
Milind AgarwalFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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