Dolphin
Dolphin

Reputation: 39055

Why has different output of function replace?

declare @tachareName varchar(200)
        ,@a varchar
set @tachareName='fsfhk,fsif,'
if(CHARINDEX(',',@tachareName)!=0)              
    --print @tachareName
    --select REPLACE(@tachareName,',',' ')--The output is:fsfhk fsif 
    set @a=REPLACE(@tachareName,',',' ')
    --REPLACE(@tachareName,',',' ')
    print @a    --The output is:f

What's wrong,i want to judge if the string has ',' symbal and repalce it with ' '.

The envirioment is SQL Server 2008.

Upvotes: 1

Views: 35

Answers (1)

gzaxx
gzaxx

Reputation: 17600

Because of your declaration of @a - you have not declared length of variable so it defaulted to 1. Thus returning only the first letter of result.

Declare @a as:

declare @a varchar(200)

Read more about char and varchar declaration on MSDN.

Upvotes: 2

Related Questions