How to easily check whether a @String contains Unicode characters or not

It’s easy! πŸ˜€


DECLARE @String NVARCHAR(MAX)
SET @String = N''

--How to easily check whether a @String contains Unicode characters or not (like Japanse characters) - simple method:
IF CAST(@String AS VARCHAR(MAX)) != @String
BEGIN
--String contains unicode character(s).
END

ELSE
BEGIN
--String does not contain unicode character(s).
END

Advertisements

2 thoughts on “How to easily check whether a @String contains Unicode characters or not

  1. Except this is exactly backward. If casting as VARCHAR does not change the string it does not contain unicode characters. If it changes then it does contain unicode. Should BE:

    IF CAST(@String AS VARCHAR(MAX)) != @String

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s