Results 1 to 3 of 3

Thread: Convert not standard text file into SQL

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Convert not standard text file into SQL

    I need to read a text file and then insert into a SQL server.

    The text file is not in standard format as below.

    ABC, 12345,
    BBB,9988, PP
    ABC, 556699,
    CCC.8899,UU

    It should be format like below:

    ABC, 12345,BBB,9988, PP
    ABC, 556699,CCC.8899,UU

    All row is starting "ABC"

    How to read this text file?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Convert not standard text file into SQL

    In what way is that not standard? Is it just that there are extra line breaks?

    The file is CSV. The fact that every row starts with ABC is pretty useful, as it would allow you to identify when a row has been wrapped. What might be more difficult is that, in your example, there aren't a fixed number of columns. The first row has 5 while the second row has only 4. That might be just a typo, though, since you separate BBB from 9988 with a comma, whereas you separate CCC from 8899 with a period. If that is real, then that is going to cause more trouble than the line breaks.

    What I would suggest is to use ReadAllLines to get the file into an array of strings, then iterate through that dealing with the extra line breaks. Normally, you could query the file directly, but since the file has extra line breaks, then it won't query quite right, so pulling it into an array of strings and fixing the array seems like the best solution. It all depends on whether there is a fixed number of columns, though.
    My usual boring signature: Nothing

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Convert not standard text file into SQL

    Just split by position of ABC, and split the splitted text by comma and insert.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width