开发者

How to user prefix 'N' for unicode with nvarchar variable in SQL Server?

开发者 https://www.devze.com 2022-12-30 03:04 出处:网络
How to user prefix \'N\' for unicode with nvarchar variable in SQL Server?For example: Given this variable:

How to user prefix 'N' for unicode with nvarchar variable in SQL Server? For example:

Given this variable:

declare @Query1 nvarchar(max) 

I can assign to it like this:

set @Query1 = N'لاحظات'

But what if I want to use N@Query1开发者_JS百科 somewhere?


You only need to use N'xyz' when you have literal text. Once the nvarchar data is in a variable or result set column of nvarchar data type you no longer need to use the N'...' notation, the system knows the data type at that point.

try it out:

DECLARE @A   nvarchar(100)
       ,@B   nvarchar(100)
SET @A = N'anything here!!'

SET @B=@A --would work the same if coming from a query result set as well

SELECT @B --will show special unicode characters


declare @nv1 nvarchar(50) = N'لاحظات', @nv2 nvarchar(50) = 'لاحظات', @v varchar(50) = N'لاحظات'     
declare @nv3 nvarchar(50) = @nv1 + ' hallo', @nv4 nvarchar(50) = @nv1 + N' hallo'

select @nv1, @nv2, @nv3, @nv4, @v


It is used with string literals to indicate the text should be treated as unicode. e.g.

DECLARE @something NVARCHAR(100)
SET @something = N'sometext'


Declare @var nvarchar(255)

Set @var = N'Hello World'
print @var

create table #tmp( columnA nvarchar(255) not null)
insert into #tmp(columnA) Values (N'Test')

Select @var = columnA from #tmp
print @var
drop table #tmp


Thanks to marc_s for his answer, that solved my problem. I highlight it here as an answer for those who can't find it.

" if @Query1 IS a NVARCHAR variable, then it IS a NVARCHAR variable and you don't need to prefix it with another 'N' to make it NVARCHAR.... just use it "

0

精彩评论

暂无评论...
验证码 换一张
取 消