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

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;

Related

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

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 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

SQL where field like ■

I have a SQL database where some data has a ■ in it. When I run a like query for that it does't find anything? I need to correct it as it stops the data transforming to xml?
WHERE [value] like '%■%'?
WHERE [value] like N'%■%'?
Query it as unicode
Some time unicode does not work, so you can follow the below steps
Copy the field value and paste it Notepad
Copy that special character
Paste between the Like statement.
I am sure it will works. Please confirm.

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

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.