Strange REPLACE behavior

  • Could someone explain to me why the following returns a double quote as the result?

    Notice that the dollar value is in double quotation marks, declared as a varchar. I just find the result here to be somewhat confusing. Yes, I know I did it in the wrong order compared to what I intended to do...

    declare @r varchar(255)

    select @r = '"$5,176,641.00"'

    select @r = replace('"','',@r)

    select @r

  • I think you answered your own question. The incorrect order of the REPLACE function parameters is the problem. You should run it like this instead:

    select @r = replace(@r,'"','')

    _________________________________
    seth delconte
    http://sqlkeys.com

  • oh I know I am getting the wrong result. I messed that part up (since corrected in the ad-hoc code this was from). What I do not understand is why the result set is a double quote even in my wrong code.

  • That's because in this syntax:

    declare @r varchar(255)

    select @r = '"$5,176,641.00"'

    select @r = replace('"','',@r)

    select @r

    you are using a double-quote as the string expression, and trying to replace every instance of '' (nothing) with the string "$5,176,641.00". There are no any instances of nothing (empty string), so the string expression remains unchanged (the double-quote that you started with). Does that make sense?

    _________________________________
    seth delconte
    http://sqlkeys.com

  • Perfectly! I knew I was just missing one little detail in my thinking! Thanks.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply