Link to home
Start Free TrialLog in
Avatar of Charlene Kerr
Charlene KerrFlag for United States of America

asked on

Remove Chinese characters in SQL

I am working with data from Hong Kong and getting alot of Chinese characters mixed in. The software I am importing into does not read these. Is there a simple way in SQL to clear out the Chinese characters from the fields?

Thanks
Avatar of Andy Marshall
Andy Marshall
Flag of United Kingdom of Great Britain and Northern Ireland image

Presumably that the acceptable characters for the software you are using are the standard alpanumerics, as opposed to languages using pictorial script such as Chinese/Japanese/Korean etc.

The 'acceptable' characters will have character codes, just as the unacceptable characters will have character codes...  so filter your data to remove the characters that are not in the acceptable 'range'.  It means a lot of processing as you will be checking every character, but it should work.  

Alternatively you you could some kind of regular expression/pattern matching.  As to how to implement that, I found a useful article on StackOverflow Strip unwanted characters from string

I modified it slightly for your needs, so give this a go:

Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    Declare @KeepValues as varchar(100) = '%[^a-zA-Z0-9]%'
    While PatIndex(@KeepValues, @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')

    Return @Temp
End

Open in new window

Avatar of Charlene Kerr

ASKER

So far, a manual search and replace is sounding much simpler.  Typically the data is just a name in alphanumeric followed by the name in Chinese characters, like:

Kobe Huang ¿¿¿

I'd be willing to do the work in Excel if that makes things simpler.

Thanks
Haha... those symbols were Chinese characters until I hit submit... :)
ASKER CERTIFIED SOLUTION
Avatar of Andy Marshall
Andy Marshall
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks