Help w/ importing flatfiles!
January 2, 2007 1:07 PM
Subscribe
Flat file importing best practices? tips? ideas?
Hi, I've been tasked with importing a few dozen flatfiles into our SQL Server database and I am beyond frustrated! If you know of an easier way/tool/or whatever, I would very much appreciate it!
The files are space delimited. Each field is enclosed by double-quote characters.
Problem is: Some of the text fields already contain double-quotes:
e.g. "Field 1" "Field 2" "Field 3="Value"" "Field 4="Value2" Extra Text" etc...
e.g.2 "Field 1" "Field 2" "Field 3="Value"" "Field 4="Value2" Extra Text" ""Field 5a" "Field5b""
SQL Server 2005's Import/Export Wizard chokes on these lines because the extra quotes throws it off. These files contain around 500,000 lines each and as far as I can tell, there are at least 10% of lines with this problem so this is a hell of a lot of lines to fix manually.
I've tried using a REGEX capable search and replace to try inteligently replaceing " w/ "" (escaped double quotes), but not sure how to reliably catch the extra quotes...
Your help would be greatly appreciated!
posted by apark to computers & internet (20 comments total)
1 user marked this as a favorite
First make sure that your dat doesn't contain the new delimiter (Search for it, all is good if it's not there.)
Now replace double-quote space double-quote with new-delimiter. Also replace double-quote newline with new-delimiter newline (or in regex, "$).
Now tell the Import Wizard that fields are delimited by new-delimiter, records by newline, and that quotes aren't special.
In other words, from this:
e.g. "Field 1" "Field 2" "Field 3="Value"" "Field 4="Value2" Extra Text" etc...
to this:
e.g. Field 1|Field 2|Field 3="Value"|Field 4="Value2" Extra Text|
Note that this won't work properly for your example two.
posted by orthogonality at 1:17 PM on January 2, 2007