Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. BULK INSERT into SQL Server from CSV with apostrophes instead the strings

BULK INSERT into SQL Server from CSV with apostrophes instead the strings

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T2102
    wrote on last edited by
    #1

    I am trying to perform a bulk insert into SQL Server where some of the text fields may contain apostrophe's, which need to be removed. Can I bulk insert from the csv and then have the last column be the file name I am inserting from? BULK INSERT MY_DB.dbo.SYMBOLS FROM 'C:\data.csv'WITH ( DATAFILETYPE = 'char', FIELDTERMINATOR = ',', ROWTERMINATOR = '\r\n' ) My workaround is to query to csv file to a dataset, remove the apostrophes, and then run a ton of insert queries. I am doing this for more than a hundred files.

    M R 2 Replies Last reply
    0
    • T T2102

      I am trying to perform a bulk insert into SQL Server where some of the text fields may contain apostrophe's, which need to be removed. Can I bulk insert from the csv and then have the last column be the file name I am inserting from? BULK INSERT MY_DB.dbo.SYMBOLS FROM 'C:\data.csv'WITH ( DATAFILETYPE = 'char', FIELDTERMINATOR = ',', ROWTERMINATOR = '\r\n' ) My workaround is to query to csv file to a dataset, remove the apostrophes, and then run a ton of insert queries. I am doing this for more than a hundred files.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Ted2102 wrote:

      query to csv file to a dataset,

      I do the same, load the data into a datatable, clean out the single quotes and BULKCOPY the datatable into SQL Server table. The target table is all varchar b/c bulkcopy can be delicate sometimes and spits the dummy regularly.

      public int BulkCopy(DataTable dtTable, string sTableName, SqlConnection oConn)
      	{
      		try
      		{
      			SqlBulkCopy oBC = new SqlBulkCopy(oConn);
      			oBC.BulkCopyTimeout = 60000;
      			oBC.DestinationTableName = sTableName;
      			oBC.WriteToServer(dtTable);
      			return dtTable.Rows.Count;
      		}
      		catch (Exception)
      		{
      			throw;
      		}
      	}
      

      Never underestimate the power of human stupidity RAH

      T 1 Reply Last reply
      0
      • T T2102

        I am trying to perform a bulk insert into SQL Server where some of the text fields may contain apostrophe's, which need to be removed. Can I bulk insert from the csv and then have the last column be the file name I am inserting from? BULK INSERT MY_DB.dbo.SYMBOLS FROM 'C:\data.csv'WITH ( DATAFILETYPE = 'char', FIELDTERMINATOR = ',', ROWTERMINATOR = '\r\n' ) My workaround is to query to csv file to a dataset, remove the apostrophes, and then run a ton of insert queries. I am doing this for more than a hundred files.

        R Offline
        R Offline
        R Giskard Reventlov
        wrote on last edited by
        #3

        Firstly, why do you need to remove the apostrophes? Are they not part of the text you are importing or are they superfluous characters that shouldn't have been there? Either way you might consider opening the csv file in code and then either doubling the apostrophes or removing them, something like (and this is a very simplistic example):

        string filePath = "full_path_to_the_csv_file";
        string text = File.ReadAllText(filePath);

        // Either:
        text = text.Replace("'", "''");
        // or
        text = text.Replace("'", string.Empty);

        File.WriteAllText(filePath, text);

        You'll need to adjust to suit but it should get you started.

        me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

        T 2 Replies Last reply
        0
        • M Mycroft Holmes

          Ted2102 wrote:

          query to csv file to a dataset,

          I do the same, load the data into a datatable, clean out the single quotes and BULKCOPY the datatable into SQL Server table. The target table is all varchar b/c bulkcopy can be delicate sometimes and spits the dummy regularly.

          public int BulkCopy(DataTable dtTable, string sTableName, SqlConnection oConn)
          	{
          		try
          		{
          			SqlBulkCopy oBC = new SqlBulkCopy(oConn);
          			oBC.BulkCopyTimeout = 60000;
          			oBC.DestinationTableName = sTableName;
          			oBC.WriteToServer(dtTable);
          			return dtTable.Rows.Count;
          		}
          		catch (Exception)
          		{
          			throw;
          		}
          	}
          

          Never underestimate the power of human stupidity RAH

          T Offline
          T Offline
          T2102
          wrote on last edited by
          #4

          Okay, then I will make all of my strings varchar for this table. Are there any problems with loading of doubles or big ints that you are aware of?

          1 Reply Last reply
          0
          • R R Giskard Reventlov

            Firstly, why do you need to remove the apostrophes? Are they not part of the text you are importing or are they superfluous characters that shouldn't have been there? Either way you might consider opening the csv file in code and then either doubling the apostrophes or removing them, something like (and this is a very simplistic example):

            string filePath = "full_path_to_the_csv_file";
            string text = File.ReadAllText(filePath);

            // Either:
            text = text.Replace("'", "''");
            // or
            text = text.Replace("'", string.Empty);

            File.WriteAllText(filePath, text);

            You'll need to adjust to suit but it should get you started.

            me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

            T Offline
            T Offline
            T2102
            wrote on last edited by
            #5

            The apostrophes are part of the text. Appreciate the help. I will this later today as well and see how well it works. I am trying to load a couple hundred csv files.

            M 1 Reply Last reply
            0
            • T T2102

              The apostrophes are part of the text. Appreciate the help. I will this later today as well and see how well it works. I am trying to load a couple hundred csv files.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              I remove the single quotes, we regularly export the data as csv file at some point and text identifiers are not supported by SSIS (I think, one of MS core technologies does not support text qualifiers "" therefor cannot deal with single quotes in the data, astonishing). So we get a cleaner result and the our users don't give a rats ass about single quotes.

              Never underestimate the power of human stupidity RAH

              R 1 Reply Last reply
              0
              • M Mycroft Holmes

                I remove the single quotes, we regularly export the data as csv file at some point and text identifiers are not supported by SSIS (I think, one of MS core technologies does not support text qualifiers "" therefor cannot deal with single quotes in the data, astonishing). So we get a cleaner result and the our users don't give a rats ass about single quotes.

                Never underestimate the power of human stupidity RAH

                R Offline
                R Offline
                R Giskard Reventlov
                wrote on last edited by
                #7

                Mycroft Holmes wrote:

                users don't give a rats ass

                Would have both said it all and been very precise :-)

                me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

                T 1 Reply Last reply
                0
                • R R Giskard Reventlov

                  Firstly, why do you need to remove the apostrophes? Are they not part of the text you are importing or are they superfluous characters that shouldn't have been there? Either way you might consider opening the csv file in code and then either doubling the apostrophes or removing them, something like (and this is a very simplistic example):

                  string filePath = "full_path_to_the_csv_file";
                  string text = File.ReadAllText(filePath);

                  // Either:
                  text = text.Replace("'", "''");
                  // or
                  text = text.Replace("'", string.Empty);

                  File.WriteAllText(filePath, text);

                  You'll need to adjust to suit but it should get you started.

                  me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

                  T Offline
                  T Offline
                  T2102
                  wrote on last edited by
                  #8

                  I am going to start working on this shortly. I decided to remove single and double quotes, so that I would get the same results if I ran a script on the file again. If I did not have the double quote issue and several other formatting problems, then I could have called a bulk load or copy afterwards. I still might make sense to clean the full data first and then do a bulk load. My problem with my data sources files is that the first field is sometimes split into several columns and the last few columns are sometimes missing. So I have a gross C++ program to remap the data to try to get the correct table structure. I noticed that the results are incorrect for Rows when double quotes are utilized. I am specifying FMT=Delimited(,) in my connection string for a csv file. My query is SELECT * FROM C:\X.csv Suppose a row contained Generic 1st "LCD" Monitor, 1, 2, 3. The query results are coming back as "Generic 1st", Null, Null, Null.

                  modified on Thursday, April 1, 2010 2:47 AM

                  1 Reply Last reply
                  0
                  • R R Giskard Reventlov

                    Mycroft Holmes wrote:

                    users don't give a rats ass

                    Would have both said it all and been very precise :-)

                    me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

                    T Offline
                    T Offline
                    T2102
                    wrote on last edited by
                    #9

                    Given the double quote issue in the data set, I cannot use the ADO .NET csv file reading capability directly. Instead I tried using ReadAllText and WriteAllText to remove the single quotes and double quotes. I keep getting an out of memory error after a few minutes of inserts. I was able to load the datasets (before discovering the double quote issue)without any memory issues. I have 4 GB of physical memory and a maximum 16 GB virtual memory setting on my laptop. My aggregate datasets should take no more than 6GB of space in SQL Server. I've tried rebooting a couple of times and that did not help. Any ideas? void Cleanse_File(System::String ^Full_Path_To_File, long & ErrorCode) { try { System::String ^text = System::IO::File::ReadAllText(Full_Path_To_File); text = text->Replace("'", ""); text = text->Replace("\"", ""); System::IO::File::WriteAllText(Full_Path_To_File, text); } catch(System::Exception ^e) { System::Console::WriteLine(e->Message); } } At this point, I am thinking of using a different csv file reader and hoping that it can handle the double quotes without mismapping columns.

                    R 1 Reply Last reply
                    0
                    • T T2102

                      Given the double quote issue in the data set, I cannot use the ADO .NET csv file reading capability directly. Instead I tried using ReadAllText and WriteAllText to remove the single quotes and double quotes. I keep getting an out of memory error after a few minutes of inserts. I was able to load the datasets (before discovering the double quote issue)without any memory issues. I have 4 GB of physical memory and a maximum 16 GB virtual memory setting on my laptop. My aggregate datasets should take no more than 6GB of space in SQL Server. I've tried rebooting a couple of times and that did not help. Any ideas? void Cleanse_File(System::String ^Full_Path_To_File, long & ErrorCode) { try { System::String ^text = System::IO::File::ReadAllText(Full_Path_To_File); text = text->Replace("'", ""); text = text->Replace("\"", ""); System::IO::File::WriteAllText(Full_Path_To_File, text); } catch(System::Exception ^e) { System::Console::WriteLine(e->Message); } } At this point, I am thinking of using a different csv file reader and hoping that it can handle the double quotes without mismapping columns.

                      R Offline
                      R Offline
                      R Giskard Reventlov
                      wrote on last edited by
                      #10

                      If the files are large you may need to read them in a line at a time.

                      me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups