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
Related
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?
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
I need to export a table, that contains a XML column (this xml my contain any special characters, so I can not use them as column delimiters) into a text file.
I am using SQL Server 2014. The XML column may contain special characters like #, |, ,, ?, tab, <cr> many things that could be used as delimiters.
I want to export the whole table. The XML is not structured internally. The max length of the column is around 6000 characters. The table has around 700k rows. And the destination is the same table in a SQL Server 2012 (lower version than the origin), they are in different networks.
I am trying to export it as a .txt file with || as column delimiter. But when I try to import this file into the destination table, it says that the text was truncated and could not be imported.
What's the best way I can do this?
All your answers you gave in your comments give me the idea that you are going the wrong way... The best approach should be linked servers:
Read here: https://msdn.microsoft.com/en-us/library/ff772782.aspx?f=255&MSPPError=-2147217396
Further information here: https://msdn.microsoft.com/en-us/library/ms190479.aspx?f=255&MSPPError=-2147217396
Try this in your SS2014
USE [master]
GO
EXEC master.dbo.sp_addlinkedserver
#server = N'YourLowerServer',
#srvproduct=N'SQL Server' ;
GO
This you need to get access:
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'YourLowerServer',
#locallogin = NULL ,
#useself = N'True' ;
GO
If this is done you can use the INSERT INTO from one server directly to the other server. Try this in your SS2014:
INSERT INTO YourLowerServer.YourDatabase.dbo.TableName(col1,col2,...)
SELECT col1,col2,... FROM dbo.TableName
If you want to get rid of your linked server after this operation use sp_dropserver (read here: https://msdn.microsoft.com/en-us/library/ms174310.aspx)
Hope this helps...
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.
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.