• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2144
  • Last Modified:

TSQL - Replace all NULL with Empty String in a Table

Is there a efficient syntax to replace all rows and columns that have NULL with the empty string?

Thank you for your help.
0
Tom Sage
Asked:
Tom Sage
  • 3
  • 3
  • 2
  • +2
1 Solution
 
drypzCommented:
Hi! Do it something like this

      SELECT
            ISNULL(e.EMP_SURNAME, '') AS LastName,
            ISNULL(e.EMP_FNAME, '') AS FirstName,
            ISNULL(e.EMP_MNAME, '') AS MidName
      FROM TABLE_EMPLOYEE e
0
 
Patrick MatthewsCommented:
Hello MisterT25,

UPDATE SomeTable
SET Col1 = COALESCE(Col1, ''), Col2 = COALESCE(Col2, ''), Col3 = COALESCE(Col3, '')
WHERE Col1 IS NULL OR Col2 IS NULL OR Col3 IS NULL

Regards,

Patrick
0
 
Anthony PerkinsCommented:
>>Is there a efficient syntax to replace all rows and columns that have NULL with the empty string?<<
Why would you want to do that?  Do you not like Nulls?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Tom SageAuthor Commented:
I should have explained what I am trying to accomplish.

I am working with an SQL table that will eventually be exported as a Tab delimited text file.  In my testing with the bcp program, it seems that rows that contain Null values will not be exported.  So, if all the Null values were replaced by Empty strings, then bcp would export all the rows rather than skipping some.

I hope this helps.  Thank you.
0
 
arnoldCommented:
update table set col1=isnull(col1,''),col2=isnull(col2,''),....,coln=isnull(coln,'')

You could try the alter table and set a default for each column '',0 based on the column type.
0
 
arnoldCommented:
Could you post the create table syntax?

alter table tablename
MODIFY (col1 type not null default='',





     coln type not null default=0;)
may work and prevent future NULL entries.
0
 
Tom SageAuthor Commented:
Arnold,

I like your idea of setting the default to an empty string.  This will eliminate the need to remove NULLs.

Thank you very much.
0
 
Anthony PerkinsCommented:
>>In my testing with the bcp program, it seems that rows that contain Null values will not be exported.<<
You need to double check this.
0
 
Tom SageAuthor Commented:
acperkins,

Thank you for your feedback.  I have found that bcp will not export all the rows in a table.  I have "guessed" that this was due to having NULL in some rows.  Because bcp does not give an error, I have no way of knowing exactly why it excluded some rows.

I would welcome your opinion on this.  I could open another question for this so you could get the points.   Let me know if you woud like for me to do this.

Thank you very much.
0
 
Anthony PerkinsCommented:
Not a problem.  If you have found a solution that works for you, that is fine with me.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now