August 22, 2011 at 9:03 am
Does anyone know of a a way to replace "null" with "" across an entire table without having to list each column one by one.
Many Thanks
August 22, 2011 at 9:13 am
nope not possible. a column must be identified, and it's new value identified for a valid UPDATE TABLE statement.
...but...you can use the table's metadata to generate the commands for you.
SELECT 'UPDATE ' + quotename(t.name) + ' SET ' + quotename(sc.name) + ' = '''' WHERE ' + quotename(sc.name) + ' IS NULL '
FROM sys.tables t
inner join sys.columns sc
on t.object_id = sc.object_id
where type_name(sc.system_type_id) in( 'varchar','char','nvarchar','nchar')
Lowell
August 22, 2011 at 9:22 am
I would ignore the where completely unless I'd know only a small % of the rows would get updated.
This will do a table scan anyways. So you might as well make it count and do all columns in 1 scan instead of 1 scan per column.
August 22, 2011 at 9:27 am
Ninja's_RGR'us (8/22/2011)
I would ignore the where completely unless I'd know only a small % of the rows would get updated.This will do a table scan anyways. So you might as well make it count and do all columns in 1 scan instead of 1 scan per column.
i thought the WHERE would be requires so you don't simply erase pre-existing data. it's not so much to try to avoid the table scan, but to set NULLS to empty string only where it's appropriate.
Lowell
August 22, 2011 at 9:38 am
I type slower than I think.
ISNULL(Colname, '')
If this was not a one time deal I would put a bunch of ors in the where to only update the rows that need it and lessen the blow a little bit on the server.
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy