views:

2210

answers:

4

I have 2 strings in SQL and the REPLACE function only works on 1 of them, why is that?

Example 1:

SELECT REPLACE('18 286.74', ' ', '')

Example 2:

SELECT REPLACE('z z', ' ', '')

Example 1's output is still "18 286.74" whereas Example 2's output is "zz". Why does SQL not react the same way to both strings?

UPDATE:

When running select replace('123 123.12', ' ', '') that works fine. still not with '18 286.74'

A: 

maybe cast is needed.

UPD: or not(on sql 2005 works fine too)

x2
Casting a varchar to a varchar, I doubt it but I'll try it
FailBoy
Nope, used both a CAST and a CONVERT, neither work
FailBoy
A: 

Are you sure it is a space? i.e. the same whitespace character that you are passing as the second argument? The code you've posted works fine for me on SQL Server 2008.

Re working on your friends PC - perhaps the whitespace got normalized when you sent it to him?

Marc Gravell
A: 

You are probably using non-breakable space.

I could reproduce it by typing ALT+0160 into the number in SELECT REPLACE('18 286.74', ' ', '')

Could you please issue this following:

SELECT CAST('18 286.74' AS BINARY), REPLACE('18 286.74', ' ', '')

by copying the '18 286.74' from REPLACE into CAST?

Quassnoi
+4  A: 
select unicode(substring('18 286.74', 3, 1))

if the code returned is 32 ... then its a space, if not, its a different unicode character and your replace ' ' wont work.

Jabezz
interesting! Thanks a million man!
FailBoy
You can always do REPLACE('18 286.74', CHAR(160), '') in the case of non-breaking spaces
Jabezz