Solved

Comma delimited CSV files?

Posted on 2000-05-11
17
681 Views
Last Modified: 2011-09-20
Hi......
I have dumped a table to a text file from SQLServer 7.
Quoted, comma delimited, CR/LF row termination.
The thing is there are commas also in thesome of the fields themselves.
This screws me up when I try and do stuff based on the comma delimit field.
Please help.
0
Comment
Question by:rickyr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +3
17 Comments
 

Expert Comment

by:allenstoner
ID: 2800523
You can put quotes around the string fields.  Then ignore commas inside the quotes.  Of course you may have quotes in string values also.  Double quotes are usually safer because of names like O'Neal.
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2800639
use a different delimiter.
Other than commas, TAB (chr9) is also an oft used delimiter.
this could still be called a .csv and fit in with the rules.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2802650
Depends on how you are processing the file.  If you are using a program on a csv with quotes around strings, you'll need to check for that in your code.  Some simplistic pseudo code for processing one line:

Read Line
While Not Done
   Look for a comma
   If Next Character is a quote
      Look for the next quote
   End If
   Look for the next comma
   If at End of Line
      Done is True
   End If
   Assign data value
End While
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 9

Expert Comment

by:david_levine
ID: 2802960
I agree with DawsonB. You ddidn't mention where the data is going, but you should use a delimiter that isn't in your data. So if you don't have tabs in any fields, you should use that. Most programs that will open the extract file should have no issues with tab delimited data. And for a bcp, you don't need to specify anything and the tab character will be used by default (if -c is specified in the bcp).

David
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2803676
If you are using VB then an easier way to split each line out would be to use Split function

Read Line
While Not Done
   Read line from file into LineVariable
   Array = split(LineVariable,vbtab)
   
End While

this will mean you don't need to actually 'look' for the delimiters. You will just need to do with each field whatever you need to do with it.

0
 
LVL 3

Author Comment

by:rickyr
ID: 2805577
I think you are right, its a pain trying to use commas. I may use TAB.
I am using a simple select statement to generate my csv file, How ( in sql) can I strip out all tabs in the cells (data itself) during the select, so that I can be guaranteed a tab free csv (except of course for where I want them as delimeters).
That should be it, thanks.
R
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2805649
Select Field1,Field2,Replace(Field3,char(9),'<TAB>')  [Field3]
From Table

This way you could later programatticaly replace your tab, but all for all this would work just as well with a comma delimiter(although I tend to stick to TAB's).

This is all well and good, but why are you not using the DTS to do this? If you have a large table then DTS would be much more efficient.

You could use the same Select satatement on the source connection, and just point it at a text file and wham bam thank you mam!





0
 
LVL 1

Expert Comment

by:barx
ID: 2809352
I suggest using a the pipe character '|'.  This character is very rarely found in data.  I also agree with bhess1 that another good idea is, you should enclose the data in quotation marks.
0
 
LVL 3

Author Comment

by:rickyr
ID: 2947023
Ok.....
I'm using | to delimit rows, and tab to delimit cols.
but guess what? there are tabs in my data. In my sql that I use to generate my csv, how can I replace any tabs that occur with a space.
This will close this Q.
Thanks in advance
R
0
 
LVL 2

Accepted Solution

by:
DawsonB earned 50 total points
ID: 2947111
select REPLACE(ColumnName,char(9),' ') as ColumnName
:-}
0
 
LVL 9

Expert Comment

by:david_levine
ID: 2947314
Or change the column separator to another character such as '/0' that's a zero.

I'd recommend doing that before updating data.

David
0
 
LVL 3

Author Comment

by:rickyr
ID: 2947699
I'm creating the csv from sqlserver, it doesn't allow me to specify anything but comma, tab, etc.
cheers!
0
 
LVL 9

Expert Comment

by:david_levine
ID: 2947807
You can use the -r and -t options of bcp to specify the column and row terminators when extracting the data from the table to a flat-file.

David
0
 
LVL 3

Author Comment

by:rickyr
ID: 2948332
select REPLACE(ColumnName,char(9),' ') as ColumnName
returns.........
Argument data type text is invalid for argument 1 of replace function.
cheers
0
 
LVL 9

Expert Comment

by:david_levine
ID: 2948391
What are you going to do with the file - open it in Excel or a like package?

Just do something like the following in a DOS/NT command prompt:

bcp dbname.owner.table out flatfile.txt -Uuserid -Ppassword -Sservname -c -t'~' -r'|'

and that will create a file that you can load into anything where you can specify the row/column delimiters
0
 
LVL 3

Author Comment

by:rickyr
ID: 3446599
Comment accepted as answer
0
 
LVL 3

Author Comment

by:rickyr
ID: 3446600
Thanks, now see my new question
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question