skyline01
skyline01

Reputation: 2099

Empty string default values for many columns

I am working in SQL Server 2008. I have a table with many columns that will not have values (at least, for the given situation). So, they will have a NULL value when I query each of them. I would like to instead make these NULL values be empty strings (i.e., ''). What is the best way to achieve this? My current idea is to set a DEFAULT value of '' on each them at the time that the table is created. However, since there are so many of them, this will be very tedious.

Upvotes: 0

Views: 59

Answers (2)

Elias
Elias

Reputation: 2632

  1. Create a table with the same structure as your current table, with a different name, and the default value as ''.
  2. Insert into that table from your original table.
  3. Delete the original table.
  4. Change the name of the new table to the original table name.

Upvotes: 0

dima
dima

Reputation: 1189

You have 2 options:

  1. As you said, give it a default value of empty string for columns you don't want to be null when you create table/add new columns.
  2. When you select nullable columns from the table you can use IsNull(ColumnName,'') which means if ColumnName is null it'll return empty string ('').

Upvotes: 1

Related Questions