|
-
Dec 7th, 2006, 11:47 AM
#1
problem importing CSV file to SQL 2k
I was given a CSV file that I need to import into a SQL2K DB.
The table is already created in the DB with the correct structure.
The problem is the text fields in the CSV aren't closed in quotes or anything, so when I import the data, any textfield that has a cr/lf in it, screws up the import, because it thinks its the next record.
Anyone have any ideas how I might be able to get this imported without a bunch of manual legwork?
-
Dec 7th, 2006, 12:03 PM
#2
Re: problem importing CSV file to SQL 2k
Are you using DTS to import it? I dont have access to SQL anymore but there was something about text qualifiers or field transformations.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 7th, 2006, 12:05 PM
#3
Re: problem importing CSV file to SQL 2k
yeah I am, but the problem is that I guess when it hits a CR/LF, it doesn't know if its the next line, or a textfield that contains a CR/LF.
The problem wouldn't exist if the text fields were enclosed in single our double ticks, however I was just given this CSV file, I had no control over its creation.
-
Dec 7th, 2006, 12:08 PM
#4
Re: problem importing CSV file to SQL 2k
How about trying bcp? Maybe it has a different option of will perform better.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 7th, 2006, 12:11 PM
#5
Re: problem importing CSV file to SQL 2k
Ah, just thought about this. What if you read the textfile into an array or such using VB and then write out to a corrected file or import into SQL directly using ADO.NET? Basically preping the file or just directly import it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 7th, 2006, 12:19 PM
#6
Re: problem importing CSV file to SQL 2k
I thought about that actually, but what logic can I employ myself to determine if the CRLF is the end of the record line, or just part of one of the textfields?
This can be done by manually going through each record, because its human readable to figure it out, however what do you think I should do in code to determine this?
-
Dec 7th, 2006, 12:23 PM
#7
Re: problem importing CSV file to SQL 2k
Read it line by line and if there is less then the expected number of commas or fields then read the next line as part of the same line. Its just a matter of translating the cognitive task of when you visually determine if the rows are continuing on or if they are the next record, into code.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 7th, 2006, 12:57 PM
#8
Re: problem importing CSV file to SQL 2k
I would personally load the file in Excel and write a quick macro to add quotes to all cells, before re-saving as CSV and doing the import with delimiters... untested macro example:
VB Code:
Dim CurrCell as Range
For Each CurrCell In ThisWorkBook.WorkSheets("[u]Sheet1[/u]").UsedRange
CurrCell.Text = """" & CurrCell.Text & """"
Next CurrCell
-
Dec 7th, 2006, 01:10 PM
#9
Re: problem importing CSV file to SQL 2k
Being a csv file doesnt mean it has to use Excel especially if its already part of an app. If its part of an app then its easier to just add some code to add quotes or some other workaround.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 7th, 2006, 02:11 PM
#10
Re: problem importing CSV file to SQL 2k
 Originally Posted by si_the_geek
I would personally load the file in Excel and write a quick macro to add quotes to all cells, before re-saving as CSV and doing the import with delimiters... untested macro example:
VB Code:
Dim CurrCell as Range
For Each CurrCell In ThisWorkBook.WorkSheets("[u]Sheet1[/u]").UsedRange
CurrCell.Text = """" & CurrCell.Text & """"
Next CurrCell
Is that necessary? I mean, isn't there an option when saving as CSV to add text quote identifiers? It's been a while since I've had to make a csv through Excel, but I thought I remembered something to that effect in there.
-tg
-
Dec 7th, 2006, 02:13 PM
#11
Re: problem importing CSV file to SQL 2k
well again I didn't make the CSV.
I think I can likely get it working using one of the mentioned methods above, but about 3 hours ago I got yanked to work on another project, so I will have to put this on hold for a few.
-
Dec 7th, 2006, 03:07 PM
#12
Re: problem importing CSV file to SQL 2k
 Originally Posted by techgnome
Is that necessary? I mean, isn't there an option when saving as CSV to add text quote identifiers? It's been a while since I've had to make a csv through Excel, but I thought I remembered something to that effect in there.
I did a quick test and didn't get any options (even in the Save As dialog), but it probably is an option somewhere!
-
Dec 8th, 2006, 12:56 AM
#13
Re: problem importing CSV file to SQL 2k
Yes,it is in there 
You can use the Workbooks.OpenText method to open a delimited file and specify the textqualifier, format for a particular column or special delimiter etc.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 8th, 2006, 08:32 AM
#14
Re: problem importing CSV file to SQL 2k
If the data elements in the CSV have CR/LF's embedded in them and they are not quoted then it's a poor CSV file. EXCEL isn't going to like it any better.
You are going to have to code a silly little app to open the file and read data in line by line. You will know that you haven't processes x-amount of columns yet - so when the CR/LF appears you know that the next line read from the file is simply a continuation of the prior line.
I hate poorly produced import files - I get them all the time.
-
Dec 8th, 2006, 09:24 AM
#15
Re: problem importing CSV file to SQL 2k
it was a CSV created automatically from a SQL Server DB that is hosted by a go daddy website account.
They don't allow remote connections to the DB, they only had a feature to "export to CSV". Its not my site, and I didn't have control to write my own ASP or ASP.NET page to create a file how I wanted it. I just got the CSV and was informed to get it into a DB.
-
Dec 8th, 2006, 01:24 PM
#16
Re: problem importing CSV file to SQL 2k
Easiest way is just to write a procedure like I suggested earlier to prep the file for DTS importing. By the time all this is hashed out to use some other method or program, you could have been done. 
Ps, godaddy.com lol
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 8th, 2006, 01:35 PM
#17
Re: problem importing CSV file to SQL 2k
yeah I know godaddy.com is budget... but they needed a *cheap* host.
I personally use discountasp for my hosting. They are a great host, even if they do cost a little more...
short of registering activeX controls on their servers, there is little you can't do with their hosting package.
anyway I think I am going to use that method Rob and szlamany mentioned which is to just write a dinky app to make it a better importable file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|