views:

130

answers:

3

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 somewhere?

A: 

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'
Ben Robinson
Yes, but I want to use with @something not with string. how can I?
Nijesh Patel
A: 
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
Jeremy
+3  A: 

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
KM