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为什么我最后打印出来的是个空值
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为什么我最后打印出来的是个空值
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
@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
2、@a应该初始化为''
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
--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
SET @a = rtrim(isnull(@a, '')) + SUBSTRING(@string, @position, 1)
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
char(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