Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Find Replacement Character as a part of a string

Tags:

sql

tsql

I need help with writing a query which will find Replacement Character in SQL table.

I have multiple cells which contain that character and I want to find all those cells. This is how the value of cell looks like this:

Thank you for your help!

like image 863
Hemus San Avatar asked Dec 01 '14 12:12

Hemus San


2 Answers

The UNICODE suggestion didn't work for me - the � character was being treated as a question mark, so the query was finding all strings with question marks, but not those with �.

The fix posted by Tom Cooper at this link worked for me: https://social.msdn.microsoft.com/forums/sqlserver/en-US/2754165e-7ab7-44b0-abb4-3be487710f31/black-diamond-with-question-mark

-- Find rows with the character
Select * From [MyTable]
Where CharIndex(nchar(65533) COLLATE Latin1_General_BIN2, MyColumn) > 0

-- Update rows replacing character with a !
Update [MyTable]
set MyColumn = Replace(MyColumn, nchar(65533) COLLATE Latin1_General_BIN2, '!')
like image 173
Fijjit Avatar answered Nov 17 '22 17:11

Fijjit


Use the Unicode function:

DECLARE @TEST TABLE (ID INT, WORDS VARCHAR(10))

INSERT INTO @TEST VALUES (1, 'A�AA')
INSERT INTO @TEST VALUES (2, 'BBB')
INSERT INTO @TEST VALUES (3, 'CC�C')
INSERT INTO @TEST VALUES (4, 'DDD')

SELECT * FROM @TEST WHERE WORDS LIKE '%' + NCHAR(UNICODE('�')) + '%'

UPDATE @TEST
SET WORDS = REPLACE(WORDS, NCHAR(UNICODE('�')), 'X')

SELECT * FROM @TEST WHERE WORDS LIKE '%' + NCHAR(UNICODE('�')) + '%'
SELECT * FROM @TEST 
like image 3
FriendlyManCub Avatar answered Nov 17 '22 17:11

FriendlyManCub