6

I have two strings in SQL and the REPLACE function only works on one 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'.

2
  • This is also working on a colleague's pc and we running the queries against the same database server Commented Aug 24, 2009 at 12:06
  • This works in SQL Server 2008, as well. Commented Aug 24, 2009 at 12:08

5 Answers 5

12

Test it the following way.

select unicode(substring('18 286.74', 3, 1))

If the code returns 32 then it's a space, if not, it's a different Unicode character and your replace ' ' won't work.

Sign up to request clarification or add additional context in comments.

2 Comments

You can always do REPLACE('18 286.74', CHAR(160), '') in the case of non-breaking spaces
@Jabezz This unicode test feature is really cool! Was really helpful in diagnosing my mystery character :)
0

maybe cast is needed.

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

1 Comment

Casting a varchar to a varchar, I doubt it but I'll try it
0

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?

Comments

0

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?

Comments

0

I was having the same issue and found that it was a char(10) (line feed). when copied out of Managment Studio it became a char(32) but in the record it was a char(10) try

Select Replace(@string, char(13), '')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.