Solved

extra space in stored proc generated table

Posted on 2012-04-09
7
185 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to reinstall SCVMM 2012 R2 - SQL errors. 5 39
disk usage reporting tools 27 52
How come this XML node is not read? 3 28
Searching a barcode number within a string. 7 25
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

756 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