Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Sqlcmd not trimming trailing white spaces
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

2 Posts

Posted - 04/28/2013 :  18:39:24  Show Profile  Reply with Quote
Hi all,

I have a sql agent job which basically runs a stored procedure using sqlcmd and saves the output to a csv file. The stored procedure selects some data and there is one column with a lot of trailing spaces. I have used -W switch with sqlcmd to trim the space. This was working fine with SQL 2008 R2.

We have recently installed 2012 and moved the sql job to the new instance. Now the issue is that the text output file generated by the sqlcmd within the sqlagent job has all the trailing white spaces. Its not getting removed. Could anyone tell me how to get rid of these spaces ? I have run the sqlcmd command from a windows command against the new sql 2012 db and it works fine. It does not work with sql agent. That's all :(

Aged Yak Warrior

549 Posts

Posted - 04/28/2013 :  19:11:36  Show Profile  Reply with Quote
Make sure that the empty spaces are not due to line feed, carriage return or some other non printable ASCII characters...
You can try LTRIM() RTRIM() functions.
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 04/29/2013 :  00:40:36  Show Profile  Reply with Quote
does the stored procedure involve any html code?

SQL Server MVP
Go to Top of Page

Starting Member

2 Posts

Posted - 04/29/2013 :  05:24:48  Show Profile  Reply with Quote
The stored procedure is a simple select with a few joins. Unfortunately I could not modify the SP as it has been supplied by a service provider.
Go to Top of Page

Yak Posting Veteran

54 Posts

Posted - 04/30/2013 :  05:36:18  Show Profile  Reply with Quote
Thinking out the box....

Create a new stored procedure of your own
In the stored procedure create a temporary table.
Insert into the temp table and call the service provider stored procedure
Return the information after removing extra characters.
Change you SQLCMD to run your SP

Create Procedure MyProcedure
Create Table #MyTable (TheData varchar(max))

Insert Into #MYTable (TheData)
EXECUTIVE ServiceProviderSP;

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000