How to user prefix 'N' for unicode with nvarchar variable in SQL Server?
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 "
精彩评论