views:

48

answers:

2

We found out that we have a bunch of french strings with incorrect characters. We know that the hexadecimal representation of the character is 0xFDFF. Is there an easy way to SELECT the rows in a table where the NVarchar field contain this character?

Thanks

+2  A: 
WRONG: select * from YourTable where YourCol like '%' + CONVERT(nchar, 0xFDFF) + '%'

EDIT: oops, (n)char(x) pitfall

right: select * from sys.sysobjects where name like '%' + CONVERT(nchar(1), 0x4800) + '%'

EDIT2:

select * from sys.sysobjects where charindex(CONVERT(nvarchar(1), 0xFDFF), name) > 0
Arthur
Somehow this doesn't work. Probably because this character is not a normal one.
Paulo Manuel Santos
select CONVERT(nchar, 0xFDFF) returns a character. What is the problem? No Results? Or a Syntax Error?
Arthur
No results, and I know at least 2 rows that have this character. I see it when I select convert(binary, FieldName, 1).
Paulo Manuel Santos
MAd a mistake, (n)char(x) pitfall
Arthur
Now I get all rows in the table... Very strange.
Paulo Manuel Santos
CONVERT(nchar(1), 0x4800) == 'H'?
Arthur
I think I need to do a binary search on the string to find it, if TSQL can't do this we2ll end up creating a consola app for this..
Paulo Manuel Santos
@Arthur, yes 0x4800 is 'H' but I think the "like" operator doesn't like the character 0xFDFF... :P
Paulo Manuel Santos
Just try it with your query and FDFFselect * from sys.sysobjects where name like '%' + CONVERT(nchar(1), 0xFDFF) + '%'
Paulo Manuel Santos
Yes - you are right - this give me also all rows
Arthur
What about this: select * from sys.sysobjects where charindex(CONVERT(nvarchar(1), 0xFDFF), name) > 0
Arthur
Wow, that works great!Can you edit your answer?
Paulo Manuel Santos
Hmm, actually it brings some rows, but not all... I'm checking better.
Paulo Manuel Santos
It only brings the rows that start with that character! Very strange!
Paulo Manuel Santos
A: 

Thanks for all the help Arthur, +1 for you.
I ended up creating a cursor and checking all characters in every row of the table to find the strange 0xFDFF character. I concatenated all ids into a variable and got my list from there.

DECLARE CURSOR ...
....
WHILE @@FETCH_STATUS = 0   
BEGIN   

     Declare @pos int
     set @Pos = 0

     while @Pos <= Len(@Str)
     begin
      if(convert(binary(2), substring(@Str, @pos, 1), 0) = 0xFDFF)
      begin
       set @ids = @ids + ',' + cast(@TheId as varchar)
       --Select @TheId, @Str
       break
      end
      set @Pos = @Pos + 1
     end

       FETCH NEXT FROM db_cursor INTO @TheId, @Str   
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

select @ids
Paulo Manuel Santos