Identifying records with special characters (Unicode)

Occasionally Data analyst may fall in a situation, where they need to identify records holding special characters or only the records having no special records at all. It is fairly simple to identify such records.

Let us start with creating a simple table and inserting both type of character values.

InsertNVarcharCharacter

 

Now logically, if you try to convert a NVARCHAR value to VARCHAR, SQL will fail to translate it and convert the special characters into “?”.

ConvertN_ToVarchar

We use the same logic to identify our records.

To find only the Non-Unicode Character use below query. Only the records which  are successfully converted into varchar will be able to match the original column.

SELECT *
FROM #t
Where CONVERT(varchar(20),CompanyName) = CompanyName

VarcharCharacter

 

To identify only the records with special character just replace = with <>

SELECT *
FROM #t
Where CONVERT(varchar(20),CompanyName) = CompanyName.

 

Let me know your views, questions or any other method of doing same. If you are novice and want to know more detail about VARCHAR and NVARCHAR, Drop your query in  comments i will pick that up for future.

2 thoughts on “Identifying records with special characters (Unicode)

Shoot Your Comments!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s