Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

extra space in stored proc generated table

Posted on 2012-04-09
7
Medium Priority
?
188 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 2000 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
Independent Software Vendors: 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

705 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