DECLARE @position int, @string char(10), @b int, @a char(10)
SET @position = 1
SET @string = '雪儿18女'
WHILE @position <= DATALENGTH(@string)
   BEGIN
     SET @b = ASCII(SUBSTRING(@string, @position, 1))
     IF @b <> 32
       BEGIN
         PRINT SUBSTRING(@string, @position, 1)
         SET @a = @a + SUBSTRING(@string, @position, 1)
       END
     SET @position = @position + 1   END
PRINT @a为什么我最后打印出来的是个空值

解决方案 »

  1.   

    DECLARE @position int, @string char(10), @b int, @a char(10)
    SET @position = 1
    SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)
             SET @a = isnull(@a, '') + SUBSTRING(@string, @position, 1) --这里
           END
         SET @position = @position + 1   END
    PRINT @a------------------
    null + 任何数或字符串 = null
      

  2.   

    DECLARE @position int, @string char(10), @b int, 
    @a varchar(100)
    SET @position = 1set @a=''SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)--select SUBSTRING(@string, @position, 1)
             SET @a = @a + SUBSTRING(@string, @position, 1)
           END
         SET @position = @position + 1   END
    print  @a
      

  3.   

    1、@a 定义为varchar(10)
    2、@a应该初始化为''
      

  4.   

    DECLARE @position int, @string char(10), @b int, @a char(10)
    SET @position = 2
    SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)
             SET @a = @a + SUBSTRING(@string, @position, 1)
           END
         SET @position = @position + 1   END
    PRINT @a
      

  5.   


    --1、@a 定义为varchar(10)
    --2、@a应该初始化为''
      
    --正确的应该是:DECLARE @position int, @string char(10), @b int, 
    @a varchar(100)
    SET @position = 1set @a=''SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)--select SUBSTRING(@string, @position, 1)
             SET @a = @a + SUBSTRING(@string, @position, 1)
           END
         SET @position = @position + 1   END
    print  @a  
    --如果@a定义为char(10),其中全是空格,再连接多少字符,前10个都是空格。--如下例子可以看出定义为char(10)的问题:DECLARE @position int, @string char(10), @b int, 
    @a char(10),@c varchar(100)
    SET @position = 1set @a=''
    set @c=@a+'test'SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)--select SUBSTRING(@string, @position, 1)
             SET @a = @a + SUBSTRING(@string, @position, 1)
           END
         SET @position = @position + 1   END
    print len(@a)
    print @c
      

  6.   

    再改一下,char(10)还要加rtrim()
    SET @a = rtrim(isnull(@a, '')) + SUBSTRING(@string, @position, 1)
      

  7.   

    DECLARE @position int, @string char(10), @b int, @a char(10)
    SET @position = 1
    SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)
             SET @a = @a + SUBSTRING(@string, @position, 1)--特殊数据处理.null?
           END
         SET @position = @position + 1   END
    PRINT @a
      

  8.   

    wangtiecheng 说的很对,确实是char这个数据类型的问题,改成varchar并把它赋初值为'',现在可以@a就可以接收到字符串了。
      

  9.   

    为什么varchar(100)可以
    char(100)不行呢
      

  10.   

    为什么varchar(100)可以
    char(100)不行呢
    ----------------------------------
    执行如下代码,就很清楚了。
    --如果@a定义为char(10),其中全是空格,再连接多少字符,前10个都是空格。--如下例子可以看出定义为char(10)的问题:DECLARE @position int, @string char(10), @b int, 
    @a char(10),@c varchar(100)
    SET @position = 1set @a=''
    set @c=@a+'test'SET @string = '雪儿18女'
    WHILE @position <= DATALENGTH(@string)
       BEGIN
         SET @b = ASCII(SUBSTRING(@string, @position, 1))
         IF @b <> 32
           BEGIN
             PRINT SUBSTRING(@string, @position, 1)--select SUBSTRING(@string, @position, 1)
             SET @a = @a + SUBSTRING(@string, @position, 1)
           END
         SET @position = @position + 1   END
    print len(@a)
    print @c