Nijesh Patel
Nijesh Patel

Reputation: 111

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

Upvotes: 11

Views: 38755

Answers (6)

Sanushi Salgado
Sanushi Salgado

Reputation: 1436

If it's a dynamic value (@Query1) & not a hardcoded string, you can try this:

N'' + @Query1

Upvotes: 0

Javi Agredo
Javi Agredo

Reputation: 1

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 "

Upvotes: 0

REXMAN
REXMAN

Reputation: 29

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

Upvotes: 2

Jeremy
Jeremy

Reputation: 4838

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

Upvotes: 0

KM.
KM.

Reputation: 103587

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

Upvotes: 11

Coding Flow
Coding Flow

Reputation: 21881

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'

Upvotes: 0

Related Questions