BCP to CSV file with commas in the data - sql

I have a BCP process that is calling a stored procedure. Typically I have been executing this stored procedure and copying the data to an Excel sheet that I specifed all columns as text and saved this as a CSV.
I need to automate this work and have been playing with the BCP command but so far have an issues. The data I have has commas in it which shifts data to the left. Is this something I can overcome with a format file or something of the sort?
I would rather not quote these in the output of the proc itself.

The BCP command has the -t switch which specifies the field terminator. In your case, it is a comma or CHR(44).
http://technet.microsoft.com/en-us/library/ms162802.aspx
To prevent MS Excel from having issues opening the file, enclose all text fields in the QUERY with double quotes "" or CHR(34).
Here is a sample query from Adventure Works.
-- Enclose text w/ possible commas in quotes
select
char(34) + AddressLine1 + char(34) as fmt_address_line1,
char(34) + City + char(34) as fmt_city,
PostalCode as postal_code
from
[AdventureWorks2012].[Person].[Address]
This should allow you to open the file in MS Excel w/o any issues.

Related

bcp query skip comma

The issue i am having is that i have data in sql columns which have "," in the data
Example column : Text,Text2,text3
so i want to skip the "," so it will not going to effect my data otherwise it just messing up the data
EXEC xp_cmdshell 'bcp "SELECT * from details queryout "\\TEst\Share\32\123Test.csv" /c /t, -T'
Since this file has quoted values and the quoted values are quoted on every record (not JUST the values that contain commas) you can use this solution.
Essentially you need to use a BCP format file with the -f option. With the format file you can specify custom field delimiters. So, in your case, the delimiters will be customized to no longer be just a comma (,) but will now include the quotes as well (",").
It gets a little tricky if the first or last fields are quoted, but that can be done too. See here:
SQL Server BCP Bulk insert Pipe delimited with text qualifier format file
or here:
SQL Server BCP Export where comma in SQL field

SQL: Copy text file to db with multiple character delimiter

I have a file that I cannot edit that uses :: as the delimiter. I'm using the following sql formula to copy the contents of the file to a postgresql database:
cur.execute("COPY table_name (col_1,col_2,col_3,col_4) FROM 'file_path' WITH DELIMITER ':'")
When I execute this code the program throws DataError: extra data after last expected column. This appears to be happening because sql is inserting a blank column when it parses the colon character for the second time. SQL doesn't allow the use of multiple character delimiters so '::' is not an option.
I'm wondering if there's a best practice for this situation. Maybe some way to ignore the second colon?

SQL Server 2014 export CSV file issues

I have searched for an answer on this, but have come up short. When exporting a table from SSMS, from time to time some of the larger files will have issues where something like an address will trigger a CR/LF and spread a record like an address across multiple rows in the exported CSV file. If I copy an paste the record directly from SSMS and paste it into another program such as word, it will do the same thing. I cant exactly put my thumb down on what is happening here. Other records will export correctly, and then all the sudden one of the records will come up like below...
Looks something like this:
1|"Apartment Katha
2|Flat No 9999 Garia Place
3|West Bengal"
But I need it like this
1|"Apartment Katha Flat No 9999 Garia Place West Bengal"
I use Unicode and " as the text qualifier.
CR and LF in SQL server are Char(10) and Char(13) Try to take out these characters from your records, and see if it works:
SELECT REPLACE(REPLACE(#str, CHAR(13), ' '), CHAR(10), ' ')

SQL View export to CSV by SQLCMD - Excel format numbers from varchar fields to exponent syntax

I export data from a sql view (SQL Server 2005) with a sql job to a csv-file:
sqlcmd -W -s ";" -E -d master -Q "SET NOCOUNT ON; SELECT * FROM [DB].[NameOfView];" -o "\\Location\File.csv"
In the sql view is a field (Type Varchar(20)) with numbers which are often longer than 15 characters. For this field I just usa a simple statement:
SELECT Fieldname AS Name FROM DB
...nothing special.
When I open the csv file excel formats the field with the long number to a format with exponent syntax:
Is there a way to edit something in the query or the sqlcmd job to say excel not to do that? I'm thinking about define the result as text and not as number in sql or something like this (but it is already a varchar field and i also tried things like ''''+ Fieldname and s.o. ... -.-)
I'm not able to change something in excel because a lot of people should have access to the csv - otherwise all of them have to make settings in excel.
They should open the csv and everything looks fine (:
Thanks for your help/ideas!
does
SELECT '"' + Fieldname + '"' AS Name FROM DB
not work? That should cause double quotes to appear in the .csv and Excel to treat it as text.
Edit: try
SELECT '"=""' + Fieldname + '"""' AS Name FROM DB
(The above answer assumes that double-quotes don't need further escaping in the SQL string, given that it uses single-quotes for text literals, so if no joy just check this. You are aiming for it to look like
"=""0.00000014567897354354"""
in the CSV)
See also https://superuser.com/questions/318420/formatting-a-comma-delimited-csv-to-force-excel-to-interpret-value-as-a-string

export query results into CSV file issue in SQL Server

I am using SQL Server 2008 Enterprise. I want to export the query result into csv file from SQL Server Management Studio. The issue is, the default csv file is comma (',') separated for each result column in each exported row, and I want to make it '\t' separated since comma exists in some of the result column values.
Any quick solutions?
thanks in advance,
George
If you really want the \t delimiter write a query like this
select
Cast(PersonCode as varchar(50)) + '\t'
+ Cast(PersonReference as varchar(50))
from People
The casts are just incase you aren't working with varchar types, but you don't need them if you are. Run this query with results to text and just paste the result into notepad or similar.
When you click "Save as" and you are prompted to enter a filename, drop down the box below the filename and select "Text (Tab delimited)"
It saves only the 1st record as Tab delimited.
I can't figur eout, how it will select all rows from query analyzer results section.