Update all rows where “NULL” as string needs to be updated to a DB NULL
up vote
-1
down vote
favorite
Is there a way to change all occurrences of a certain value within SQL regardless of column?
I have a table with ~200 columns which was imported from a text file. The NULL values came through as the string value 'NULL' and occur in most columns within the table. Is there a way to convert those values to true NULL values? I would like to avoid using UPDATE on each individual column is possible.
sql sql-server tsql
add a comment |
up vote
-1
down vote
favorite
Is there a way to change all occurrences of a certain value within SQL regardless of column?
I have a table with ~200 columns which was imported from a text file. The NULL values came through as the string value 'NULL' and occur in most columns within the table. Is there a way to convert those values to true NULL values? I would like to avoid using UPDATE on each individual column is possible.
sql sql-server tsql
5
Fix the file and re-import.
– jarlh
Nov 8 at 15:19
Tag your question with the database you are using.
– Gordon Linoff
Nov 8 at 15:24
The files were created through a python script. It's 72 csv files and I haven't been able to import NaN values from python to SQL successfully.
– Steve Harshman
Nov 8 at 15:27
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Is there a way to change all occurrences of a certain value within SQL regardless of column?
I have a table with ~200 columns which was imported from a text file. The NULL values came through as the string value 'NULL' and occur in most columns within the table. Is there a way to convert those values to true NULL values? I would like to avoid using UPDATE on each individual column is possible.
sql sql-server tsql
Is there a way to change all occurrences of a certain value within SQL regardless of column?
I have a table with ~200 columns which was imported from a text file. The NULL values came through as the string value 'NULL' and occur in most columns within the table. Is there a way to convert those values to true NULL values? I would like to avoid using UPDATE on each individual column is possible.
sql sql-server tsql
sql sql-server tsql
edited Nov 8 at 17:45
Joshua Huber
2,6631122
2,6631122
asked Nov 8 at 15:18
Steve Harshman
112
112
5
Fix the file and re-import.
– jarlh
Nov 8 at 15:19
Tag your question with the database you are using.
– Gordon Linoff
Nov 8 at 15:24
The files were created through a python script. It's 72 csv files and I haven't been able to import NaN values from python to SQL successfully.
– Steve Harshman
Nov 8 at 15:27
add a comment |
5
Fix the file and re-import.
– jarlh
Nov 8 at 15:19
Tag your question with the database you are using.
– Gordon Linoff
Nov 8 at 15:24
The files were created through a python script. It's 72 csv files and I haven't been able to import NaN values from python to SQL successfully.
– Steve Harshman
Nov 8 at 15:27
5
5
Fix the file and re-import.
– jarlh
Nov 8 at 15:19
Fix the file and re-import.
– jarlh
Nov 8 at 15:19
Tag your question with the database you are using.
– Gordon Linoff
Nov 8 at 15:24
Tag your question with the database you are using.
– Gordon Linoff
Nov 8 at 15:24
The files were created through a python script. It's 72 csv files and I haven't been able to import NaN values from python to SQL successfully.
– Steve Harshman
Nov 8 at 15:27
The files were created through a python script. It's 72 csv files and I haven't been able to import NaN values from python to SQL successfully.
– Steve Harshman
Nov 8 at 15:27
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
A single update may not be too painful:
update t
set col1 = nullif(col1, 'NULL'),
col2 = nullif(col2, 'NULL'),
. . .;
You can generate the code in SQL or a spreadsheet by querying INFORMATION_SCHEMA.COLUMNS
(or similar) for string columns.
add a comment |
up vote
1
down vote
You can use dynamic sql to build out the update script...
DECLARE @update_sql NVARCHAR(MAX) = N''
SELECT
@update_sql = CONCAT(@update_sql, N',
mt.', c.name, N' = NULLIF(mt.', c.name, N', ''NULL'')')
FROM
sys.columns c
WHERE
c.object_id = OBJECT_ID(N'dbo.MyTable')
AND c.collation_name IS NOT NULL; -- easy way to make sure you're only looking at columns that can hold test data.
SET @update_sql = CONCAT(N'
UPDATE mt SET',
STUFF(@update_sql, 1, 1, ''), N'
FROM
dbo.MyTable mt;')
PRINT(@update_sql);
You'll end up with output formatted like the following...
UPDATE mt SET
mt.column_9 = NULLIF(mt.column_9, 'NULL'),
mt.column_10 = NULLIF(mt.column_10, 'NULL'),
mt.column_11 = NULLIF(mt.column_11, 'NULL'),
mt.column_14 = NULLIF(mt.column_14, 'NULL'),
...
mt.column_165 = NULLIF(mt.column_165, 'NULL'),
mt.column_166 = NULLIF(mt.column_166, 'NULL'),
mt.column_167 = NULLIF(mt.column_167, 'NULL'),
mt.column_168 = NULLIF(mt.column_168, 'NULL')
FROM
dbo.MyTable mt;
Note... The PRINT command is limited to 8000 characters of ASCII and 4000 characters of unicode. So, if you notice that the output script is being truncated, post back, I have a "long print" procedure that get around that limitation.
add a comment |
up vote
0
down vote
use the merge statement and set null for all matching rows which is fasters and efficient way to do it.
add a comment |
up vote
0
down vote
There is no way to do this without doing an update on each individual column.
There are shortcuts to writing such an update, like right-click>script as... or dynamic sql, but so far that's not what you've asked.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
A single update may not be too painful:
update t
set col1 = nullif(col1, 'NULL'),
col2 = nullif(col2, 'NULL'),
. . .;
You can generate the code in SQL or a spreadsheet by querying INFORMATION_SCHEMA.COLUMNS
(or similar) for string columns.
add a comment |
up vote
2
down vote
A single update may not be too painful:
update t
set col1 = nullif(col1, 'NULL'),
col2 = nullif(col2, 'NULL'),
. . .;
You can generate the code in SQL or a spreadsheet by querying INFORMATION_SCHEMA.COLUMNS
(or similar) for string columns.
add a comment |
up vote
2
down vote
up vote
2
down vote
A single update may not be too painful:
update t
set col1 = nullif(col1, 'NULL'),
col2 = nullif(col2, 'NULL'),
. . .;
You can generate the code in SQL or a spreadsheet by querying INFORMATION_SCHEMA.COLUMNS
(or similar) for string columns.
A single update may not be too painful:
update t
set col1 = nullif(col1, 'NULL'),
col2 = nullif(col2, 'NULL'),
. . .;
You can generate the code in SQL or a spreadsheet by querying INFORMATION_SCHEMA.COLUMNS
(or similar) for string columns.
answered Nov 8 at 15:24
Gordon Linoff
743k32285390
743k32285390
add a comment |
add a comment |
up vote
1
down vote
You can use dynamic sql to build out the update script...
DECLARE @update_sql NVARCHAR(MAX) = N''
SELECT
@update_sql = CONCAT(@update_sql, N',
mt.', c.name, N' = NULLIF(mt.', c.name, N', ''NULL'')')
FROM
sys.columns c
WHERE
c.object_id = OBJECT_ID(N'dbo.MyTable')
AND c.collation_name IS NOT NULL; -- easy way to make sure you're only looking at columns that can hold test data.
SET @update_sql = CONCAT(N'
UPDATE mt SET',
STUFF(@update_sql, 1, 1, ''), N'
FROM
dbo.MyTable mt;')
PRINT(@update_sql);
You'll end up with output formatted like the following...
UPDATE mt SET
mt.column_9 = NULLIF(mt.column_9, 'NULL'),
mt.column_10 = NULLIF(mt.column_10, 'NULL'),
mt.column_11 = NULLIF(mt.column_11, 'NULL'),
mt.column_14 = NULLIF(mt.column_14, 'NULL'),
...
mt.column_165 = NULLIF(mt.column_165, 'NULL'),
mt.column_166 = NULLIF(mt.column_166, 'NULL'),
mt.column_167 = NULLIF(mt.column_167, 'NULL'),
mt.column_168 = NULLIF(mt.column_168, 'NULL')
FROM
dbo.MyTable mt;
Note... The PRINT command is limited to 8000 characters of ASCII and 4000 characters of unicode. So, if you notice that the output script is being truncated, post back, I have a "long print" procedure that get around that limitation.
add a comment |
up vote
1
down vote
You can use dynamic sql to build out the update script...
DECLARE @update_sql NVARCHAR(MAX) = N''
SELECT
@update_sql = CONCAT(@update_sql, N',
mt.', c.name, N' = NULLIF(mt.', c.name, N', ''NULL'')')
FROM
sys.columns c
WHERE
c.object_id = OBJECT_ID(N'dbo.MyTable')
AND c.collation_name IS NOT NULL; -- easy way to make sure you're only looking at columns that can hold test data.
SET @update_sql = CONCAT(N'
UPDATE mt SET',
STUFF(@update_sql, 1, 1, ''), N'
FROM
dbo.MyTable mt;')
PRINT(@update_sql);
You'll end up with output formatted like the following...
UPDATE mt SET
mt.column_9 = NULLIF(mt.column_9, 'NULL'),
mt.column_10 = NULLIF(mt.column_10, 'NULL'),
mt.column_11 = NULLIF(mt.column_11, 'NULL'),
mt.column_14 = NULLIF(mt.column_14, 'NULL'),
...
mt.column_165 = NULLIF(mt.column_165, 'NULL'),
mt.column_166 = NULLIF(mt.column_166, 'NULL'),
mt.column_167 = NULLIF(mt.column_167, 'NULL'),
mt.column_168 = NULLIF(mt.column_168, 'NULL')
FROM
dbo.MyTable mt;
Note... The PRINT command is limited to 8000 characters of ASCII and 4000 characters of unicode. So, if you notice that the output script is being truncated, post back, I have a "long print" procedure that get around that limitation.
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use dynamic sql to build out the update script...
DECLARE @update_sql NVARCHAR(MAX) = N''
SELECT
@update_sql = CONCAT(@update_sql, N',
mt.', c.name, N' = NULLIF(mt.', c.name, N', ''NULL'')')
FROM
sys.columns c
WHERE
c.object_id = OBJECT_ID(N'dbo.MyTable')
AND c.collation_name IS NOT NULL; -- easy way to make sure you're only looking at columns that can hold test data.
SET @update_sql = CONCAT(N'
UPDATE mt SET',
STUFF(@update_sql, 1, 1, ''), N'
FROM
dbo.MyTable mt;')
PRINT(@update_sql);
You'll end up with output formatted like the following...
UPDATE mt SET
mt.column_9 = NULLIF(mt.column_9, 'NULL'),
mt.column_10 = NULLIF(mt.column_10, 'NULL'),
mt.column_11 = NULLIF(mt.column_11, 'NULL'),
mt.column_14 = NULLIF(mt.column_14, 'NULL'),
...
mt.column_165 = NULLIF(mt.column_165, 'NULL'),
mt.column_166 = NULLIF(mt.column_166, 'NULL'),
mt.column_167 = NULLIF(mt.column_167, 'NULL'),
mt.column_168 = NULLIF(mt.column_168, 'NULL')
FROM
dbo.MyTable mt;
Note... The PRINT command is limited to 8000 characters of ASCII and 4000 characters of unicode. So, if you notice that the output script is being truncated, post back, I have a "long print" procedure that get around that limitation.
You can use dynamic sql to build out the update script...
DECLARE @update_sql NVARCHAR(MAX) = N''
SELECT
@update_sql = CONCAT(@update_sql, N',
mt.', c.name, N' = NULLIF(mt.', c.name, N', ''NULL'')')
FROM
sys.columns c
WHERE
c.object_id = OBJECT_ID(N'dbo.MyTable')
AND c.collation_name IS NOT NULL; -- easy way to make sure you're only looking at columns that can hold test data.
SET @update_sql = CONCAT(N'
UPDATE mt SET',
STUFF(@update_sql, 1, 1, ''), N'
FROM
dbo.MyTable mt;')
PRINT(@update_sql);
You'll end up with output formatted like the following...
UPDATE mt SET
mt.column_9 = NULLIF(mt.column_9, 'NULL'),
mt.column_10 = NULLIF(mt.column_10, 'NULL'),
mt.column_11 = NULLIF(mt.column_11, 'NULL'),
mt.column_14 = NULLIF(mt.column_14, 'NULL'),
...
mt.column_165 = NULLIF(mt.column_165, 'NULL'),
mt.column_166 = NULLIF(mt.column_166, 'NULL'),
mt.column_167 = NULLIF(mt.column_167, 'NULL'),
mt.column_168 = NULLIF(mt.column_168, 'NULL')
FROM
dbo.MyTable mt;
Note... The PRINT command is limited to 8000 characters of ASCII and 4000 characters of unicode. So, if you notice that the output script is being truncated, post back, I have a "long print" procedure that get around that limitation.
edited Nov 8 at 17:28
answered Nov 8 at 17:20
Jason A. Long
3,4801412
3,4801412
add a comment |
add a comment |
up vote
0
down vote
use the merge statement and set null for all matching rows which is fasters and efficient way to do it.
add a comment |
up vote
0
down vote
use the merge statement and set null for all matching rows which is fasters and efficient way to do it.
add a comment |
up vote
0
down vote
up vote
0
down vote
use the merge statement and set null for all matching rows which is fasters and efficient way to do it.
use the merge statement and set null for all matching rows which is fasters and efficient way to do it.
answered Nov 8 at 15:40
Rahul Neekhra
514426
514426
add a comment |
add a comment |
up vote
0
down vote
There is no way to do this without doing an update on each individual column.
There are shortcuts to writing such an update, like right-click>script as... or dynamic sql, but so far that's not what you've asked.
add a comment |
up vote
0
down vote
There is no way to do this without doing an update on each individual column.
There are shortcuts to writing such an update, like right-click>script as... or dynamic sql, but so far that's not what you've asked.
add a comment |
up vote
0
down vote
up vote
0
down vote
There is no way to do this without doing an update on each individual column.
There are shortcuts to writing such an update, like right-click>script as... or dynamic sql, but so far that's not what you've asked.
There is no way to do this without doing an update on each individual column.
There are shortcuts to writing such an update, like right-click>script as... or dynamic sql, but so far that's not what you've asked.
answered Nov 8 at 15:50
Tab Alleman
24.9k52440
24.9k52440
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210732%2fupdate-all-rows-where-null-as-string-needs-to-be-updated-to-a-db-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Fix the file and re-import.
– jarlh
Nov 8 at 15:19
Tag your question with the database you are using.
– Gordon Linoff
Nov 8 at 15:24
The files were created through a python script. It's 72 csv files and I haven't been able to import NaN values from python to SQL successfully.
– Steve Harshman
Nov 8 at 15:27