Comments on: A bug in Microsoft SQL Server’s replace() function http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/ Stay curious! Mon, 13 May 2013 05:55:40 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: JohnLeo Carton http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-19040 JohnLeo Carton Fri, 07 Jan 2011 13:37:03 +0000 http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-19040 Why does replace fail with this

select replace(‘aaaaa’,'a’,'b’)

the result is ‘aaaab’ when it should be ‘bbbbb’

Best regards
JohnLeo Carton

]]>
By: Anon http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-7336 Anon Thu, 17 May 2007 17:20:51 +0000 http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-7336 This solved the problem for me…

select replace(fld, N’ ‘, N’ ‘)
from table

This would help when fld is an nvarchar instead of varchar.

]]>
By: Phred http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-112 Phred Wed, 22 Mar 2006 03:32:48 +0000 http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-112 I tripped over this bug too when trying to “unabbreviate” data in a view. The following failed because of the bug:

SELECT DISTINCT 
     REPLACE(REPLACE(department, 'sch of ', 'School of '), 'sch ', 'School of ') AS  deptExpanded, [position]
FROM         vWebDirectory
ORDER BY deptExpanded, [Position]

All instances of ‘sch’ were replaced by ‘School of’, including within ‘School’ etc.

An ugly workaround was developed that may be useful in similar circumstances, see below;

SELECT DISTINCT 
                      CASE WHEN charindex('sch of ', department)  0 THEN replace(department, 'sch of ', 'School of ') WHEN charindex('sch ', department) 
                       0 THEN replace(department, 'sch ', 'School of ') ELSE department END AS deptExpanded, [position]
FROM         vWebDirectory
ORDER BY deptExpanded, [Position]
]]>
By: Xaprb http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-21 Xaprb Thu, 15 Dec 2005 21:41:41 +0000 http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-21 Microsoft’s Abdy Iman has responded to tell me someone is looking into the bug. They’ve confirmed it and that it is still present in SQL Server 2005.

]]>
By: Xaprb http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-8 Xaprb Wed, 23 Nov 2005 17:40:15 +0000 http://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/#comment-8 The documentation for space states the return type is char but the following still succeeds:

if replace('two  spaces', space(2), space(1)) = 'two spaces'
    print 'Replacement worked'
else
    print 'Replacement failed'

Apparently the return value of the function is either not as stated, or the input to the replace function isn’t where it’s being trimmed.

]]>