Solved

extra space in stored proc generated table

Posted on 2012-04-09
7
187 Views
Last Modified: 2012-06-27
I have the proc below.  The data portion of the returned table has an extra blank on the end of then data, except for the last row.   I need to get rid of this blank.  Even though I've added ltrim and rtrim, I'm still getting it.  How can I get rid of it?

CREATE FUNCTION [dbo].[Split_PValues]
(
      @RowData nvarchar(2000)
)  
RETURNS @RtnValue table
(
      Id int identity(1,1),
      Data varchar(100)
)
AS  
BEGIN
      Declare @Cnt int
      Set @Cnt = 1
      Declare @delimiter nvarchar(7)
      Set @delimiter = 'Annual:'

      While (Charindex(@delimiter,@RowData)>0)
      Begin
            IF @Cnt > 1      Insert Into @RtnValue (data)
            
            Select Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@delimiter, @RowData)-1)))
            
            Set @RowData = Substring(@RowData,Charindex(@delimiter, @RowData) + len(@delimiter),len(@RowData))
            Set @RowData = ltrim(rtrim(@RowData))                        
                                                
            Set @Cnt = @Cnt + 1
      End
            
      Insert Into @RtnValue (data)
      Select Data = ltrim(rtrim(@RowData))

      Return
END
0
Comment
Question by:HLRosenberger
[X]
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
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 37826001
I need to get rid of this blank.  Even though I've added ltrim and rtrim, I'm still getting it.  How can I get rid of it?
That would be because it is not a blank, but rather some other character.  If I was to bet I would suspect it is a Cr as in CHAR(13).  If you cannot do away with it, you can do something like this:

Insert Into @RtnValue (data)
Select Data = ltrim(rreplace(@RowData, CHAR(13), ''))
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37829337
Good idea.  I did not think of that.

Actually the last character is a 9.  So I did this:

Select Data = ltrim(rtrim(replace(@RowData, CHAR(9), ' ')))

but it did not seem to help.  I must be doing something wrong.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37829371
Ah, I finally got it.

Set @RowData = Substring(@RowData,Charindex(@delimiter, @RowData) + len(@delimiter),len(@RowData))

Set @RowData = ltrim(rtrim(@RowData))

Set @RowData = replace(@RowData, CHAR(9), '')
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 37829527
Thahnks!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37830524
Actually the last character is a 9.
That would be a Tab.  I would try and fix it in the source.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37830569
The source is a commercial database.   I cannot fix it there
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37830714
The source is a commercial database.   I cannot fix it there
I meant when you import it.  If you have no control over that, than I agree you will have to resort to some cheesy workaround.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

615 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