[X]

T-SQL Check if String contains unicode character(s)

T-SQL - Checking for Unicode characters

Here is a code in T-SQL which will check if a string contains UNICODE characters:

Lets create a table variable:

 

declare @temp table (Name varchar(100), Address1 nvarchar(200))

 

Now we will insert two records, one of which contains ascii and one non-ascii character.

 

Now run below query:

 

insert into @temp(Name, Address1) values ('Vedant',N'A PIéL')

--select * from @temp
declare @test nvarchar(200)='',@test2 varchar(200)=''
select @test+=Address1 + ',', @test2+=Address1 + ',' from @temp
print substring(@test,0,len(@test))

print substring(@test2,0,len(@test2))

if CAST(@test as varchar(max))<>@test
begin
print 'String contains unicode character(s).'
end
else
begin
print 'String does not contain unicode character(s).'
end

--
insert into @temp(Name, Address1)
values
('Umesh',N'वृहस्पतिवार')

select @test+=Address1 + ',', @test2+=Address1 + ',' from @temp
print substring(@test,0,len(@test))

print substring(@test2,0,len(@test2))

if CAST(@test as varchar(max))<>@test
begin
print 'String contains unicode character(s).'
end
else
begin
print 'String does not contain unicode character(s).'
end

 

 

Output:

 

(1 row(s) affected)
A PIéL
A PIéL
String does not contain unicode character(s).

(1 row(s) affected)
A PIéL,A PIéL,वृहस्पतिवार
A PIéL,A PIéL,???????????
String contains unicode character(s).

 

blog comments powered by Disqus

Posts By Month