Why does my file seem displaced when I send my SQL Server results to a file?

I have the following SQL

IF	OBJECT_ID('tempdb..#TempTable_Lockbox_File_Header_Record')		IS NOT	NULL	DROP TABLE	#TempTable_Lockbox_File_Header_Record
;
IF	OBJECT_ID('tempdb..#TempTable_Lockbox_Batch_Header_Record')		IS NOT	NULL	DROP TABLE	#TempTable_Lockbox_Batch_Header_Record
;
--
--	Temporary Table [#TempTable_Lockbox_File_Header_Record] will be used to house the File Header 
--	Record data columns for Lockbox file format
--
CREATE TABLE	[#TempTable_Lockbox_File_Header_Record]
(
RecordType							CHAR(2),
RecordDescription					CHAR(12)
)
;

--
--	Load Temp Table [#TempTable_Lockbox_File_Header_Record] with the correct data elements to
--	satisfy the Lockbox File Header Record format
--

INSERT
INTO	[dbo].[#TempTable_Lockbox_File_Header_Record]
		(
		RecordType,
		RecordDescription
		)
SELECT	'FH',
		'TEST'
;

SELECT	*
FROM	[dbo].[#TempTable_Lockbox_File_Header_Record]
;

When I send my query results to a file in SQL Server Management Studio, how come I'm seeing the following in Notepad++?

FH   TEST

"FH" which I thought should be in a CHAR(2) data column is there but "TEST" seems to start in Column 6...not column 3 as I would have expected. I was expecting...

FHTEST

Can anyone please help me out with this?

Thanks for your review and am hopeful for a reply.

August 24th, 2015 5:02pm

It is created as tab-delimited file, so each column is delimited with tab.

Also, what do you have in Tools/Options for SQL Server/Query/Results to Text? You need to not have the Include Column Headers checked. Also play with the Output format. Keep in mind that these options will also work on a new query, not the existing query in the existing query w

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 5:35pm

I am not sure why you expect TEST to appear in column 3. It appears that from your sample code that you are trying to produce a file with a certain format. SSMS is not intended for anything like this. If you want to produce a file with a certain format, you could possibly use BCP with a format file, but best is probably to write a program or an SSIS package.

August 24th, 2015 5:36pm

Either follow Erlands suggestions, or create a single fixed width column in SSMS:

SELECT	LEFT('FH'+REPLICATE(' ',3),3)+LEFT('TEST'+REPLICATE(' ',5),5)

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 6:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics