views:

80

answers:

2

I have a column in the database (SQL Server 2005) that has data with a "\0" at the end. When querying in SQL Server, this character is not visible and does not "seem" to exist. When I look in my C# code, the character is there. This character is causing an error on our website, and we need it removed from all the affected rows.

Is there a sql query I can write to easily remove this character from all the records that are affected? I can get all the affected records, but I don't have a way to update the record to a new value (without the "\0").

UPDATE: This seems to work:

Select * from TABLE
where UNICODE(SUBSTRING(naughtyField, LEN(naughtyField), 1)) = 0

So:

Update TABLE
SET naughtyField = SUBSTRING(naughtyField, 1, LEN(naughtyField) - 1)
where UNICODE(SUBSTRING(naughtyField, LEN(naughtyField), 1)) = 0
+3  A: 

UPDATE tbl SET col = REPLACE(col,char(0),'')

Edit: Just to redeem this answer! Might be useful for the more general case that a string has embedded \0s.

CREATE FUNCTION dbo.RemoveNullChars 
(
    @string NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX) WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
DECLARE @Result NVARCHAR(MAX)
SET @Result = ''

DECLARE @counter INT

SET @counter = 0

WHILE (@counter <= LEN(@string))
    BEGIN
     IF UNICODE(SUBSTRING(@string,@counter,1)) <>  0 
        SET @Result = @Result + SUBSTRING(@string,@counter,1)
    SET @counter = @counter + 1    
    END
RETURN @Result
END

Then

 UPDATE tbl SET col = dbo.RemoveNullChars (col)
Martin Smith
+2  A: 

Does...

UPDATE mytable
SET myfield = REPLACE(myfield, CHAR(0), '')

...work?

SUBSTRING(naughtyfield, 1, LEN(naughtyfield) - 1) on those fields that are null-terminated works - but be careful not to use it on non-NULL terminated strings or you'll be losing data.

Will A
No. From my quick test. `create table #t (col varchar(50)) insert into #t values ('hgjh' + char(0) + 'gghg' ) UPDATE #t SET col = REPLACE(col,char(0),'')`
Martin Smith
Does LEN(naughtyfield) return the length including the null-terminator in MSSQL?
Will A
Yes, Len('The string in the column') = X, Len(naughtyfield) = X + 1
Martin
Use `SUBSTRING(naughtyfield, 1, LEN(naughtyfield) - 1)` on those fields that are null-terminated - that works.
Will A
I am not sure ALL the fields will have it, so I might be cutting off good data
Martin
"I can get all the affected records" - can you?
Will A
Well, yeah, but I would like a cleaner solution, one that gives me better confidence I am not removing something I shouldn't be
Martin
When I cast to VARBINARY, I get 00 on the end of the string. You didn't did you?
Will A
No, I get randomness
Martin
Take a look @ http://stackoverflow.com/questions/2828333/what-is-the-null-character-literal-in-tsql - there are a few ideas there that could be useful to you - sounds like this is a collation specific issue.
Will A
@Will (a) Move your substring comment into your answer so he can accept it! (b) I tried some ideas from that question you linked but they didn't seem to work for nvarchar columns `declare @t table(col nvarchar(50)) insert into @t values (N'日' + char(0) + N'国' ) UPDATE @t SET col = REPLACE(cast(col COLLATE SQL_Latin1_General_CP1_CI_AS as nvarchar(max)), CHAR(0), '') select col, CAST(col as varbinary) from @t`
Martin Smith