Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i read and import piple delimited files to the database. File has data which has line break. 

I tried SQL Sever File Import wizzard as well as CSV Helper Library. Could you please help me how do I read the data successfully which has line break. 

example,
unique_id|name|notes
123|John|Subject: Meet the new interns
Dear team,
I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns
456|Rit|Subject: Meet the new interns
Dear team,
​​​​​​​I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns


What I have tried:

SQL Server File Import, CSV Helper.
Posted
Updated 27-Mar-23 15:18pm
v2
Comments
PIEBALDconsult 27-Mar-23 18:32pm    
First, see -- https://datatracker.ietf.org/doc/rfc4180/ . Send that URL to your colleague and see if he sees sense.
Second, if the sender refuses to format the file properly, then you may need to resort to desperate measures... Regular Expressions.

The problem is that it's "badly formed" CSV data - not the pipe character instead of comma, but the lack of double quotes to delimit multiline strings.

When you want to use "broken strings", you have to use double quotes around all string elements:
unique_id|name|notes
123|"John"|"Subject: Meet the new interns
Dear team,
I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns"
456|"Rit"|"Subject: Meet the new interns
Dear team,
​​​​​​​I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns"
Otherwise the reader has no idea where the column data is meant to end - is it meant to be
"123"
"John"
"Subject: Meet the new interns
Dear team,
I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns"

"456"
"Rit"
"Subject: Meet the new interns
Dear team,
​​​​​​​I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns"
Or
"123"
"John""Subject: Meet the new interns
Dear team,
I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns
456"

"Rit"
"Subject: Meet the new interns
Dear team,
​​​​​​​I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns"
-null-
Most (non-trivial) CSV readers support this, as well as double quotes / column separators that appear in strings.
 
Share this answer
 
I am consuming the file and I have no control over the file. At this point I am trying to write my own logic. Thank you sir.
 
Share this answer
 
Comments
PIEBALDconsult 27-Mar-23 18:30pm    
Please do not make a comment as a solution.
Reply to the post or use use the Improve question button.
Behold the power of Regular Expressions!

C#
System.Text.RegularExpressions.MatchCollection mat = 
System.Text.RegularExpressions.Regex.Matches 
( 
@"123|John|Subject: Meet the new interns
Dear team,
I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns
456|Rit|Subject: Meet the new interns
Dear team,
I'm happy to inform you of the commencement of our summer internship program. We recruited 10 interns
" 
,
  @"(^|\G)(?'ID'\d+)\|(?'Name'\w+)\|(?'Note'.*?)((\r\n(?=\d+\|))|$)"
,
  System.Text.RegularExpressions.RegexOptions.Compiled
  |
  System.Text.RegularExpressions.RegexOptions.ExplicitCapture
  |
  System.Text.RegularExpressions.RegexOptions.Singleline
) ;

for ( int i = 0 ; i < mat.Count ; i++ )
{
  System.Console.WriteLine 
  (
    "\r\n{0}  ID:{1}  Name:{2} Note:{3}"
  ,
    i
  ,
    mat [ i ].Groups [ "ID"   ].Value 
  ,
    mat [ i ].Groups [ "Name" ].Value 
  ,
    mat [ i ].Groups [ "Note" ].Value 
  ) ;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900