即使使用适当的功能后,也不会删除尾随空格

时间:2015-03-20 12:38:41

标签: sql sql-server stored-procedures

我只是想从字符串变量中删除尾随空格。 以下是示例代码:

Set @Tree_ID='1.23     '

Set @Tree_ID = LTRIM(RTRIM(@Tree_ID))

但令我惊讶的是,领先的空间并没有被删除。我分别做了同样的工作,它工作得很好。但不知何故,它并没有修复我的存储过程中的空间。

以下是我的存储过程

 ALTER procedure [dbo].[SP_Web_Calculate_Interest] -- 23,1,'2014-04-01','2015-03-19','','kiran.divate','COMP7',1

@FGRPNo int,
@Company_No int,

@From_Date datetime,
@To_Date datetime,
@CC_TID varchar(200),
@UserName varChar(100),
@HostName varChar(100),
@DispMode int=1

 As
 Begin
 Declare @Tree_ID char(30)
 Declare @TempTable Table(Group_No smallint,Company_No smallint,Group_Name    char(50),Tree_ID varchar(30),Is_Fixed char(1))

 If @FGRPNo > 0
 Begin
    if @DispMode =1
     Begin
       Insert into @TempTable(Group_No ,Company_No,Group_Name ,Tree_ID ,Is_Fixed ) 
        select distinct g.Group_No,g.Company_No, g.group_name,g.Tree_ID,g.Is_Fixed
          from account_group  G  inner join Account a 
        on a.Tree_ID like '%.' + ltrim(rtrim(CAST(g.Group_No as varchar(10))))  + '.%' 
            where (a.company_no=@Company_No  and a.account_no 
           in (  select Account_No 
          from Interest_Percent 
            where Company_No=@Company_No and Int_Rate >0 and effective_Date  between @From_Date and @To_Date) 

            and g.group_no=@FGRPNo


          and (g.company_no = 0 or g.company_no=@Company_No))

           order by g.group_no
    End
   Else
  Begin
                Insert into @TempTable(Group_No ,Company_No,Group_Name   ,Tree_ID ,Is_Fixed ) 
          select distinct g.Group_No,g.Company_No,    g.group_name,g.Tree_ID,g.Is_Fixed
           from account_group  G  inner join Account a 
            on a.Tree_ID like '%.' + ltrim(rtrim(CAST(g.Group_No as varchar(10))))  + '.%' 
            where (a.company_no=@Company_No  and a.account_no 
           in (  select Account_No 
          from Interest_Percent 
           where Company_No=@Company_No and Int_Rate >0 and effective_Date between @From_Date and @To_Date) 

               and g.Parent_group_no= @FGRPNo


           and (g.company_no = 0 or g.company_no=@Company_No))

             order by g.group_no

      End

  End
 Else
  Begin
             Insert into @TempTable(Group_No ,Company_No,Group_Name ,Tree_ID  ,Is_Fixed ) 
         select distinct g.Group_No,g.Company_No,  g.group_name,g.Tree_ID,g.Is_Fixed
            from account_group  G  inner join Account a 
             on a.Tree_ID like '%.' + ltrim(rtrim(CAST(g.Group_No as varchar(10))))  + '.%' 
             where (a.company_no=@Company_No  and a.account_no 
           in (  select Account_No 
         from Interest_Percent 
          where Company_No=@Company_No and Int_Rate >0 and effective_Date between @From_Date and @To_Date) 




       and (g.company_no = 0 or g.company_no=@Company_No))

       order by g.group_no
  End

 Declare @CompNo int

 Declare cur_temp SCROLL CURSOR FOR 

   Select Group_No ,Tree_ID,Company_No  from @TempTable 

 FOR READ ONLY

OPEN cur_temp 
FETCH first FROM cur_temp into @FGRPNo ,@Tree_ID,@CompNo
WHILE (@@fetch_status <> -1)
     Begin
     if @CompNo =0 
        Begin
         Set @Tree_ID = Convert(varchar(10),@Company_No) + '.' + @Tree_ID  
        end
        Set @Tree_ID = REPLACE(@Tree_ID, ' ', '')

        Exec SP_Calculate_Interest @FGRPNo,@Company_No,@Tree_ID,@From_Date,@To_Date,@CC_TID,@UserName,@HostName 
End

  Close cur_Temp

deallocate cur_Temp

--Exec SP_Web_GetAccountInterest @FGRPNo,@Tree_ID ,@UserName ,@HostName ,@Company_no  ,'G',@CC_TID ,@BalanceAmount output  ,@InterestAmount output  
--Select @BalanceAmount 
--Select @InterestAmount

if @DispMode >1
Begin
  Declare @TempTable2 Table(Account_No int,Account_Name char(50),Tree_ID char(30),parent_group_no smallint)

  Insert into @TempTable2(Account_No ,Account_Name,Tree_ID ,parent_group_no ) 
  select Distinct A.Account_No, A.Account_Name, A.Tree_ID, A.parent_group_no 
    from account A  Where A.parent_group_no = @FGRPNo  and A.company_no=@Company_No and a.account_no 
            in (select Account_No from Interest_Percent
             where Company_No=@Company_No  and Int_Rate >0 
             and effective_Date between @From_Date and @To_Date) order by A.Account_No

    Declare @Acc_No int 
    Declare cur_temp2 SCROLL CURSOR FOR 

    Select Account_No ,Tree_ID  from @TempTable2 

    FOR READ ONLY

        OPEN cur_temp2 
        FETCH first FROM cur_temp2 into @Acc_No ,@Tree_ID
        WHILE (@@fetch_status <> -1)
             Begin

                Set @Tree_ID = REPLACE(@Tree_ID, ' ', '')
                    Exec SP_Calculate_Interest @Acc_No,@Company_No,@Tree_ID,@From_Date,@To_Date,@CC_TID,@UserName,@HostName 
                End

        Close cur_Temp2

        deallocate cur_Temp2

End


End

部分代码:

  if @CompNo =0 
    Begin
     Set @Tree_ID = Convert(varchar(10),@Company_No) + '.' + @Tree_ID  
    end
    Set @Tree_ID = REPLACE(@Tree_ID, ' ', '')

Set @Tree_ID = Convert(varchar(10),@Company_No) + '.' + @Tree_ID的@Tree_ID值为1.23,带有尾随空格,因此如果if Set @Tree_ID = REPLACE(@Tree_ID, ' ', '')语句结束,它是否应该立即删除空格 但是当我在调试器中添加watch时,它仍然是相同的!!

6 个答案:

答案 0 :(得分:1)

编辑:问题是@Tree_ID正在使用数据类型CHAR(30)!

CHAR总是填充足够的空间来使用它的全长。如果您不想使用尾随空格,则需要使用VARCHAR。

答案 1 :(得分:1)

@Tree_IDchar(30)。所以总是30个字符,无论你如何修剪或替换空格。

您可以尝试使用varchar。 Declare @Tree_ID varchar(30)

答案 2 :(得分:0)

您可以使用REPLACE

SELECT REPLACE(@Tree_ID, ' ', '')

尝试使用它:

LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(@Tree_ID, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')))

答案 3 :(得分:0)

declare @Tree_ID varchar(100)
Set @Tree_ID='1.23     '

Set @Tree_ID = LTRIM(RTRIM(@Tree_ID))

print '|'+@Tree_ID+'|'

结果:|1.23|

它工作正常,但作为建议。小心选项卡空格或输入空格(例如,当文本来自备忘录时)

使用TAB空间查看此ex:

declare @Tree_ID varchar(100)
Set @Tree_ID='1.23      '

Set @Tree_ID = LTRIM(RTRIM(@Tree_ID))

print '|'+@Tree_ID+'|'

答案 4 :(得分:0)

    CREATE FUNCTION RemovewhiteSpaces
    (
        @InputStr varchar(8000)
    )
    RETURNS varchar(8000)
    AS
    BEGIN
    declare @ResultStr varchar(8000)
    set @ResultStr = @InputStr
    while charindex(' ', @ResultStr) > 0
        set @ResultStr = replace(@InputStr, ' ', '')

    return @ResultStr
    END

select dbo.RemoveAllSpaces('1.23    1 ')

RESULT 
1.231

并在您的商店程序中申请......可能工作精细

答案 5 :(得分:0)

我替换下面的代码:

 Declare @Tree_ID char(30)

使用:

 Declare @Tree_ID varchar(30)

它对我来说很好。因此,这意味着LTRIMRTRIMReplace函数都没有问题,这些函数不用于静态分配(char),当我将其更改为{时{1}}它工作正常