October 20, 2022 at 2:24 pm
I have a column with data where i am getting character �
Example - data looks like Student�
Tried to use replace function, didnt worked, Any function to replace this?
October 20, 2022 at 3:51 pm
If you are unfamiliar with the ascii character set, you will need to look that up.
The hammer would be to process each character in the string with ASCII() and only copy those characters that come back in a specific range to a new string. When you are done copy your new string back over your old string. Continue ...
Not sure there is an elegant way that doesn't come down to what I suggest.
<><
Livin' down on the cube farm. Left, left, then a right.
October 20, 2022 at 5:00 pm
What is the datatype of the column that symbol appears in?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 20, 2022 at 5:59 pm
Does this help? If there are other characters you may be able to identify their NCHAR value with UNICODE.
DECLARE @Column NVARCHAR(100)
SET @Column = CONCAT('Student' COLLATE Latin1_General_BIN , NCHAR(65533))
SELECT @Column,
UNICODE(RIGHT(@Column, 1)),
REPLACE(@Column, NCHAR(65533), N'' COLLATE Latin1_General_BIN)
October 20, 2022 at 6:01 pm
If it's varchar and not nvarchar...
That's probably not the actual character -- rather what SSMS is translating it to for display.
You can use a tally table or tally function (see Jeff Moden's links) to cross apply the column to get the position & ascii code of the offending character(s) -- e.g.,
DECLARE @string VARCHAR(255) = (SELECT StringColumn FROM dbo.MyTable WHERE ID = 7);
SELECT Number,SUBSTRING(@string,Number,1),ASCII(SUBSTRING(@string,Number,1))
FROM util.Tally(0,LEN(@string)) numbers
WHERE Number > 0
-- Or if table is small or you can add a filter to limit to offending rows):
SELECT Number,SUBSTRING(StringColumn,Number,1),ASCII(SUBSTRING(StringColumn,Number,1))
FROM dbo.MyTable
CROSS APPLY util.Tally(0,LEN(StringColumn )) numbers
WHERE Number > 0
October 21, 2022 at 3:34 pm
Asking one more time...
What is the datatype of the column that symbol appears in?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 21, 2022 at 4:21 pm
Its nvarchar(50)
October 21, 2022 at 4:54 pm
What is the collation?
Does it look that way in SSMS (or another application/client?)
If SSMS, check this article for how to set grid to display unicode characters. It suggests Arial Unicode MS. If you want a fixed-width font, search engine should provide some options.
If application, does the app support unicode?
(Just trying to determine if replace is really necessary, or if this is just a display issue -- the fact the column is defined as nvarchar implies you want to support unicode)
October 21, 2022 at 5:07 pm
What is the actual character? What does UNICODE(RIGHT(ColumnName, 1)) return?
That diamond character is the unicode Specials replacement character U+FFFD:
If that actually is the value stored, then it's likely the collation doesn't support the "real" character or application/process that inserted it didn't.
October 21, 2022 at 6:33 pm
What is the actual character? What does UNICODE(RIGHT(ColumnName, 1)) return? That diamond character is the unicode Specials replacement character U+FFFD: If that actually is the value stored, then it's likely the collation doesn't support the "real" character or application/process that inserted it didn't.
Spot on and that's why I was asking what the datatype of the column is.
It would have helped your cause if you posted an example of your REPLACE() code... like this... which clearly demonstrates the problem...
--===== This doesn't work
SELECT Test = REPLACE(N'Student�',N'�',N'')
;
Test
-------------
Student�
Then, someone could have come along with a clear demonstration of a solution... like this... without several posts of just suggestions (which are still great IF you understand the suggestions)...
--===== Force a "common collation" on all the parts to do the match
SELECT ThisWorks = REPLACE(N'Student�' COLLATE Latin1_General_BIN2,N'�',N'')
;
Results:
ThisWorks
------------
Student
--Jeff Moden
Change is inevitable... Change for the better is not.
November 5, 2022 at 8:26 pm
At McFarlandParkway,
Are you all set? Did the solution I posted work for you?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply