tmss_it_dept
asked on
Easiest way to go through database and convert empty strings to NULL
Hello all. I have a database that has a lot of data in it already,and just realized that some of the empty fields are zero-length strings and some are NULL values because of some of my code in my ASP pages. I have corrected my code, but now need to fix the data that is already in the database. What is the easiest way to go though all my tables and replace all empty strings with NULL values?
Thanks in advance,
Mike
Thanks in advance,
Mike
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
- there is no direct function for setting multiple columns to NULL without specifying the columnname however you can generate the UPDATE statements list using the following SQL.
- this works on SQL Server but you can use the same concept on other database in getting the list of tablename and columnname of text, char and varchar datatype to create a list of UPDATE statement:
SELECT 'UPDATE ' + o.name + ' SET ' + c.name + ' = NULL WHERE LEN(LTRIM(RTRIM(' +
c.name + '))) = 0'
FROM sysobjects o
JOIN syscolumns c ON o.id = c.id
JOIN systypes t ON c.xtype = t.xtype
WHERE o.xtype = 'U'
AND (t.name like '%char' or t.name like '%text')
- this works on SQL Server but you can use the same concept on other database in getting the list of tablename and columnname of text, char and varchar datatype to create a list of UPDATE statement:
SELECT 'UPDATE ' + o.name + ' SET ' + c.name + ' = NULL WHERE LEN(LTRIM(RTRIM(' +
c.name + '))) = 0'
FROM sysobjects o
JOIN syscolumns c ON o.id = c.id
JOIN systypes t ON c.xtype = t.xtype
WHERE o.xtype = 'U'
AND (t.name like '%char' or t.name like '%text')
ASKER
Thanks guys. I only had 3 tables that really fell into this category, so the solution provided by matthewspatrick and OP Zaharin was the easiest for me to do. Also OP Zaharin, the last comment you gave code to generate the update statenments, when I tried that one, it said something about a Syntax error in FROM clause. It is highlighting the first JOIN statement. Will this work in Access?
Thanks again,
Mike
Thanks again,
Mike
- no it won't work in Access. you need to do it via your system code such as ASP or VB. here is one: http://vbcity.com/forums/t/154628.aspx
exec dbo.sp_FixTableBlankValues
Open in new window