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.










share|improve this question



















  • 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














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.










share|improve this question



















  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer



























    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.






    share|improve this answer





























      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.






      share|improve this answer



























        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.






        share|improve this answer




















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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.






          share|improve this answer
























            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.






            share|improve this answer






















              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.






              share|improve this answer












              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 8 at 15:24









              Gordon Linoff

              743k32285390




              743k32285390






















                  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.






                  share|improve this answer


























                    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.






                    share|improve this answer
























                      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.






                      share|improve this answer














                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 8 at 17:28

























                      answered Nov 8 at 17:20









                      Jason A. Long

                      3,4801412




                      3,4801412




















                          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.






                          share|improve this answer
























                            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.






                            share|improve this answer






















                              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.






                              share|improve this answer












                              use the merge statement and set null for all matching rows which is fasters and efficient way to do it.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 8 at 15:40









                              Rahul Neekhra

                              514426




                              514426




















                                  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.






                                  share|improve this answer
























                                    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.






                                    share|improve this answer






















                                      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.






                                      share|improve this answer












                                      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.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 8 at 15:50









                                      Tab Alleman

                                      24.9k52440




                                      24.9k52440



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                          Edmonton

                                          Crossroads (UK TV series)