rickyr
asked on
Comma delimited CSV files?
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.
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.
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.
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.
Other than commas, TAB (chr9) is also an oft used delimiter.
this could still be called a .csv and fit in with the rules.
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
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
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
David
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.
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.
ASKER
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
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
Select Field1,Field2,Replace(Fiel d3,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!
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!
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or change the column separator to another character such as '/0' that's a zero.
I'd recommend doing that before updating data.
David
I'd recommend doing that before updating data.
David
ASKER
I'm creating the csv from sqlserver, it doesn't allow me to specify anything but comma, tab, etc.
cheers!
cheers!
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
David
ASKER
select REPLACE(ColumnName,char(9) ,' ') as ColumnName
returns.........
Argument data type text is invalid for argument 1 of replace function.
cheers
returns.........
Argument data type text is invalid for argument 1 of replace function.
cheers
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
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
ASKER
Comment accepted as answer
ASKER
Thanks, now see my new question