Take the 2-minute tour ×

In SQL or T-SQL how can I go through a table and set fields that are NULL to empty strings if the type is textual and 0 if the fields are integer types.

Thanks for any help, I do not even know where to begin.

share|improve this question
Do you know the table, columns, column data types ahead of time? Do the columns need to be changed to prevent nulls from being entered in the future? –  Esoteric Screen Name Aug 8 '12 at 16:33

4 Answers 4

up vote 1 down vote accepted

What you're suggesting would only work as a "one off".

If you always want those fields to be populated going forward you can set a default value on the table.

share|improve this answer

Well if it's just one table use a basic update query:

SET NumericColumn = ISNULL(NumericColumn,0), 
StringColumn= ISNULL(StringColumn,'')
share|improve this answer

You can use IsNull function in T-SQL,

Syntax :

ISNULL ( check_expression , replacement_value )
share|improve this answer

The function coalesce( expression_1 , expression_2 , ... ) is also useful in this context.

It evaluates the specified expressions from left to right and returns the value of the first expression that evaluates to a value other than null, or null if all the specified expressions evaluate to null.

This can be useful, especially in conjunction with left joins and table rotations.

share|improve this answer

Your Answer


