xp_SendMail Padded spaces in text file

Posted on 2002-06-10
Last Modified: 2008-02-01
The following SQL is run in an overnight job, the purpose to run a SQL query and email the results in a text file to a valid email address..

use Master
exec xp_sendmail @recipients = "",
                    @Message = "Please Find Enclosed The Information Pack Requests",
           @separator  = ",",
           @Query = "USE DB1 Select Forename, surname, age From TABLE1",
              @subject = "TEST ONLY",
           @attach_results = 'TRUE',
           @no_header ='True',
           @width = 1000

The process runs fine but the fields within the text file show as follows

john       ,smith      ,21  ,

Is there anyway I can stop this padding of spaces???
and so the text file would read as

The fields are all varchars with different lengths which I think may be part of the problem. The additional spaces dont sem to be stored in the DB field so I'm assuming its looking at the field max length and padding accordingly..

Any ideas??


PS this question is also repeated in other topic areas but points will be given for only one answer across all areas..
Question by:Madmarlin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 7066611
Try using ltrim and rtrim functions:

exec xp_sendmail @recipients = "",
                   @Message = "Please Find Enclosed The Information Pack Requests",
          @separator  = ",",
          @Query = "USE DB1 Select ltrim(rtrim(Forename), ltrim(rtrim(surname), ltrim(rtrim(age) From TABLE1",
             @subject = "TEST ONLY",
          @attach_results = 'TRUE',
          @no_header ='True',
          @width = 1000
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7066617
RTrim, ltrim and cast functions doen't work :-(

My comments In VB:

Accepted Solution

lozzamoore earned 50 total points
ID: 7066633
How about:

exec xp_sendmail @recipients = "",
                  @Message = "Please Find Enclosed The Information Pack Requests",
         @separator  = ",",
         @Query = "USE DB1 Select Forename + ', ' + surname + ', ' + age From
            @subject = "TEST ONLY",
         @attach_results = 'TRUE',
         @no_header ='True',
         @width = 1000

With cast if age is not char, (which is should be!)

Author Comment

ID: 7266937
Did find this out myself but since you also suggested this then you can have the points

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rolling count by date, hour query 7 31
Creating a View from a CTE 15 46
SQL Job Failed 6 31
Datatable / Dates ? 4 31
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question