SQL - "Save results as CSV" - use comma instead of Semi-Colon - sql

Hey, i need to export a query result to a CSV, but i get a semi-colon as a delimiter.
I went to:
Tools - Options - Query Results - SQL Server - Results to Text
and set
Output format: Custom delimiter
Custom delimiter: ,
and i still get the semicolon. when i return to the options, the original configuration is set.
I'm using MS SQL Management Studio 2008.
Thanx for the help

This one of the options that needs you to close that query window and open a new one before it takes effect.

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;

right to left string in SQL server

Consider below query containing both Persian(a right to left language) and English(a left to right language):
SELECT 'نرم افزار SQL سرور'
the required result is this string :
سرور SQL نرم افزار
Is there any function or any other way to converting string from ltr to rtl??
It is required to add N before string literal: SELECT N'نرم افزار SQL سرور'. This is needed to correctly interpret contained Unicode characters. (Source)
Important: In some cases, please avoid using standard copy-paste in order to put SELECT into SSMS command window. This could affect the RTL/LTR order. Instead, try to open correctly composed file using File > Open.
And regarding your comment:
the result should be : سرور SQL نرم افزار`
I admit I understand RTL writing system only partially, but from what I can see, Persian words are put to the output exactly in order as you entered them (even if reading right to left). Could you show me based on Unicode Bidirectional Algorithm or similar standards document why the word order should be changed by SQL Server? Shouldn't be change you expect made by preprocessing on another place, sending expected string form SELECT N'سرور SQL نرم افزار'? I see no point why just SQL SELECT should perform the change. If it did, what would happen if you feed result of such a SELECT into another SELECT? Another transformation? I have reasons to think that SQL server is interpreting your input technically correctly.
Hint: maybe you can try to surround your RTL text by different Directional formatting characters.
Please try the same SELECT with MySQL server at SQL Fiddle. Different server and technology, but the same result as Microsoft SQL Server gave.
Result from SSMS with MS SQL Server:
Conclusion: in order to get expected result, please form the input accordingly.
Related: Transformation of word order you expected can be done by appropriate settings in user interface.
When we add digit with english this will again not work following solution will work
SELECT nchar(8234)+ N' 33-M ' + N'کلینک کمرہ نمبر' +nchar(8236) + N'میں تشریف لائیں'

SQL (T-SQL, SQL Server Mgmnt Studio): Creating comma delimited .txt file with commas in fields

Background: I have to create a report that will be run regularly to send to an external entity. It calls for a comma delimited text file. Certain fields required for the report contain commas (I can easily parse the commas out of the name fields, but errant commas in the address and certain number fields are trickier). I have no control over the database design or input controls.
I know how to get a comma-delimited text file of query results from SQL Server Management Studio. But the commas in the fields screw everything up. I can change the delimiting character and then get the fields right in Excel, but that's just a workaround - it needs to be able to meet specifications automatically.
This report previously ran on an antiquated DBMS - I have a copy of an old report, and the fields are all enclosed in double quotes ("...."). This would work - though I don't know how the external users parse the fields (not my problem) - but I'm too dumb to figure out how to do it in t-sql.
Thanks!
You can use the Export Data task, but if you must try getting these results from Management Studio after running a query, go to Tools>Options, find the settings for Grid Output and check the box to delimit fields that contain field separators. This option will only take effect when you open a new query window.

tsql : outputting each record to their own text file

is there a simple way to just output each record in a select statement to write to its own file?
for example, if you have the tsql query in sql server 2005,
select top 10 items, names + ':' + address from book
and you ended up with 10 text files with the individual name and addresses in each file.
is there a way to do this without writing an extensive spWriteStringToFile procedure? I'm hoping there is some kind of output setting or something in the select statement.
thanks in advance
SQL returns the result set first, there's no opportunity in there for writing records to specific files until afterwards.
Being SQL Server 2005, it's possible you could use a SQLCLR (.NET 2.0 code) function in a SQL statement without having to make a separate application.
In SSMS, you can do a results to file, but that wouldnt split each record out into its own file. I pretty sure you cannot do this out of the box, so it sounds like you will be rolling your own solution.
You'd do this in some client, be it Java, VBA or SSIS typically.

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.