export query results into CSV file issue in SQL Server - sql

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.

Related

Custom delimiter in SQL Server set in "Results to Text" options doesn't work

I am interested in saving the result of a SQL query, like a SELECT statement, as a pipe (|) delimited text file in SSMS.
I can do that using the Export wizard.
However, it seems there's a simpler method by setting a "Custom delimiter" in Tools>Options under Query Results>SQL Server>Results to Text as shown here:
Then, if I specify "Results to Text" or "Results to File", I am supposed to get the pipe delimited result.
This is also outlined here:Obtaining Pipe Delimited Results from SQL Server using SSMS
But, doing this I still get the usual output with either Results to Text or File.
I don't know what I'm missing or doing wrong.
Thanks for your help.
Thanks to #AlexYu, the answer is simply applying the "turning it off and on" trick. Closing and re-opening SSMS is required before the change in this setting takes effect.
Incidentally, disconnecting and re-connecting to the SQL server doesn't work in this case. I have done some rudimentary tests and it seems this also applies to other options here, for example under Query Results>SQL Server>Results to Grid.
I was using Microsoft SQL Server Management Studio v17.9.1, build number 14.0.17289.0.
I think "Query Results" is able to delimit the results by a custom character, only if all the datatype is varchar. I did what you wrote in the description and additionally I casted all the columns to varchar.
SELECT TOP (1000) CAST([COL_A] as varchar) as 'COL_A'
,CAST([COL_B] as varchar) as 'COL_B'
,CAST([COL_C] as varchar) as 'COL_C'
FROM TABLE_NAME;

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

How to remove Nulls from Save As in SQL Server Management Studio

I have created a variable that is type table inside a stored procedure. At the end of the procedure I am selecting all the rows in the table and displaying them. When I right click on the headers and select "Save As" it allows me to change the type to All Files and save the file as a text file. This works fine except that the columns that have NULLS in them saves as NULLS. I want it to fill NULLs in with spaces.
I've been trying to find a way to create a file using a stored procedure but most things indicate to use SSIS but I can't figure out how to use SSIS with a variable that is a table instead of using an actual table.
If I could either replace nulls with spaces or use a stored procedure to do the same thing it would be great. I can not use tab or comma delimited as the final product has to be a flat file that each column uses the same amount of characters as is declared in the column headers. Padded with spaces.
Thanks for any help you are able to offer.
Cheers
P.S. I am using SQL Server 2012 Management Studio
The easy way to do this would be to convert the NULLs to spaces in your SELECT statement.
SELECT COALESCE(yourcolumn, '')
Put the COALESCE clause around every column that has NULLs in it.
Using COALESCE article link
If the last thing you do in the stored procedure is Select * From TempTable then you can use that SP in an OleDb source component. Change from Table or View to Sql Command and use the Exec (sp_SomeName) syntax. This will create a pipe that you can connect to a destination component, such as flat file.
I have seen many issues over the years doing Save Results As... I will only use this for informal 'quick check' files and not for anything considered 'live' or 'production' data.
Here is a good blog that also shows how to use parameters.
http://geekswithblogs.net/stun/archive/2009/03/05/mapping-stored-procedure-parameters-in-ssis-ole-db-source-editor.aspx

SQL Server 2005 select query not retrieving complete text from a column

Maybe this would be very simple, but I have no idea why this is happening.
To put in simple way, I have inserted a row into a database table which contains a ntext column called content. String value which got in to this column content has a text length of 6889 characters. However when I do a basic select query based on id, it do retrieve the row - but not the complete text from the column Content. All I see in SQL Server Management Studio for this column when I do a select query is the text with characters 43679.
There is no issue with data being inserted and I could see all the text being displayed in front end application. The issue is only when I do a select query, and copy the text to notepad, I do see fewer characters. Anything with respect to SQL Server settings/paging/statistics? Appreciate if anyone could help me out with this.
thanks,
KK
I encountered similar problem, and I found that SSMS is unable to fully retrieve column that is longer than 43680 (in output to Grid mode).
So the work around is output it as XML (unlimited length):
SELECT convert(xml,'<xml><![CDATA[' + cast([your column] as varchar(max)) + ']]></xml>')
Then do a quick search and replace (< to < , > to > ) . You can now copy the result to Notepad++ to view all the content.
SSMS returns a maximum number of characters to the grid or text window of every text based column: SSMS menu: options: Query results node : SQL node : Results to grid / Results to text
I'm sure there is a good reason for this: probably SSMS would fall over even more regularly than it does now.
Hope this helps... no such thing as a silly question

How to select all data from oracle database table in human readable format?

Sorry if my questions sounds silly, but I never worked with Oracle databases.
If I'm executing select * from table in MS Management Studio I have 3 options to select that data. I'm using grid and sometimes text. Using grid you're able to easily copy this into Excel spreadsheet.
When I'm selecting data from Oracle database table I have to format each column, but when I'm executing select * from table, I'm getting something very unreadable.
I'm using SQL Developer. Is there any way to retrieve data in more friendly view?
Thanks
Use Run Statement command (green arrow or Ctrl+Enter).
If you want export data, you can right click on table and select Export..
First, SQL Developer allows you to export selected data into many format, including the "new" XLSX spreadsheet format. So you do not really need to worry about formatting.
But if you want to do it yourself, you can try to select the data in CSV format that is very easy to convert to Excel spreadsheet. Something like this:
SELECT '"' || column1 || '","' || column2 || '"'
FROM table
WHERE ...
You might want to convert dates to some universal format (like YYYY-MM-DD) and you need to enclose text with double quotes if they contain double quotes.
You might want to consider using Toad for Oracle, The free version gives you the option to view your data in Grid Format and you will also be able to export it out into Excel or csv.