Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6797595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:37:39+00:00 2026-05-26T18:37:39+00:00

I have a CSV file with 6 millions rows. Each line is made up

  • 0

I have a CSV file with 6 millions rows. Each line is made up of the same format eg/

I,h,q,q,3,A,5,Q,3,[,5,Q,8,c,3,N,3,E,4,F,4,g,4,I,V,9000,0000001-100,G9999999990001800000000000001,G9999999990000001100PDNELKKMMCNELRQNWJ010, , , , , , ,D,Z

I have 2 columns in a table.

The first column should be field 27 in the CSV and the second column should be the whole line in the CSV file.

I have tried to set up a format file but cannot get it working.

Is it even possible to do this sort of mapping?

Here is what I have:

BULK INSERT Staging FROM 'C:\Data.txt' 
   WITH 
   (
      FIELDTERMINATOR =',',
      ROWTERMINATOR ='\n',
      KEEPNULLS,
      formatfile='C:\format.fmt'
   )

This is my format file

9.0
2
1       SQLCHAR       0       40     ","     27     Col27               SQL_Latin1_General_CP1_CI_AS
2       SQLCHAR       0       200     "\r\n"  1     Col1               SQL_Latin1_General_CP1_CI_AS

In terms of comparison, I have this working in SQLite which takes 2min 35secs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:37:40+00:00Added an answer on May 26, 2026 at 6:37 pm

    You could crack this nut a couple of ways but the way I did it was to bulk insert the WHOLE csv file into a temp table via dynamic SQL:

    CREATE TABLE #BulkLoadData(
        RecordData NVARCHAR(max)
    )
    
    
    SET @SQL = 'BULK INSERT #BulkLoadData FROM ''' + @SourceFileFullPath + ''' '
    SET @SQL = @SQL + 'WITH (FORMATFILE = ''' + @UPXInputFileBulkLoadFormat + 'UPXInputFileBulkLoadFormat.xml'', TABLOCK, ROWS_PER_BATCH = 2500 ) '
    
    EXECUTE (@SQL)
    

    Then you can insert the data into the target table like this:

    INSERT INTO dbo.TargetTable
    SELECT dbo.fnParseString(27, ',', RecordData), RecordData
    

    You’ll need to create a parse function like so:

    CREATE FUNCTION [dbo].[fnParseString]
    (
        @Section SMALLINT,
        @Delimiter CHAR,
        @Text VARCHAR(MAX)
    )
    RETURNS VARCHAR(8000)
    AS
    
    BEGIN
    DECLARE @startindex NUMERIC(18,0),
         @length NUMERIC(18,0),
         @FieldPosition INT
    
     SET @FieldPosition = ABS(@Section) - 1
     SET @startindex = 0
    
    
     WHILE @FieldPosition != 0
     BEGIN
        SET @FieldPosition = @FieldPosition - 1
         SET @startindex = CHARINDEX(@Delimiter, @Text, @startindex + 1) 
     END     
    
    
     SET @Text = SUBSTRING(@Text, @startindex + 1, LEN(@Text) - @startindex)
     SET @Text = SUBSTRING(@Text, 0, CHARINDEX(@Delimiter, @Text))
    
     RETURN @Text
    END
    

    Hope that helps! If you need help with the format file let me know.

    Here is the format file contents:

    <?xml version="1.0"?>
    <BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <RECORD>
        <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\n" MAX_LENGTH="8000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
      </RECORD>
      <ROW>
        <COLUMN SOURCE="1" NAME="RecordData" xsi:type="SQLVARYCHAR"/>
      </ROW>
    </BCPFORMAT>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file. Each line is made up of the same format
I have 2 big CSV file with millions of rows. Because those 2 CSVs
I have a .csv file containing over 70 million lines of which each line
I have 3 million rows customers.csv file. When I import it in SQL 2008,
I have a csv file of the format: 270291014011 ED HARDY - TRUE TO
I have a CSV file with several entries, and each entry has 2 unix
I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have csv file with a line that looks something like this: ,,,,,,,,,, That's
I have a very large CSV file (Millions of records) I have developed a
I have csv files with the following format: CSV FILE a , b ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.