1

Well, I know there are some similar questions in the forum, but still there is no explicit solution.

I want to add N to a variable to store double byte(Japanese) into db table, but I do not know how to add the N to a variable.

The value is already in the variable @value, which it gets its value(Japanese) automatically from a CSV file.

What I want to do is insert the value into a db table with correct Japanese chars. So I did

   declare @finalValue nvarchar(255);
   set @finalValue=N@value; --obviously, it is NOT correct.
   --insert ........

what should I do for inserting correct @finalValue into table?

4
  • Does it work without the N? Commented Feb 27, 2014 at 22:42
  • I want to insert japanese into the table, which the value is from a CSV file. '@value' is declared as nvarchar to store the value from the csv file. But at this moment, if I insert '@value', still shows "???" in the db table. So I have to add something like N to '@value', then insert it into the table. Is there any way to achieve that? Or an alternative way? Commented Feb 28, 2014 at 4:43
  • How are you reading the text file? Sounds like you are not using the proper code page when reading it. Commented Feb 28, 2014 at 14:16
  • "But at this moment, if I insert '@value', still shows "???" in the db table." How are you looking at in in the the db? Is it possible the characters stored correctly, but isn't displaying correctly? Commented Mar 1, 2014 at 14:36

2 Answers 2

1

The N'some text' is a way of defining a double byte string literal. You don't use it with Transact-SQL variables.

What is the the data type of your @value variable? In most cases you can do this:

create table foo ( dbcs_string nvarchar(2000) not null )
...
declare @my_variable varchar(2000)
set @my_variable = 'the quick brown fox jumped over the lazy dog.'
...
insert foo ( dbcs_string ) values ( @my_variable)

and the conversion will be done implicitly.

You can, however, explicitly coerce you variable into the proper type using the convert() function:

convert(nvarchar(2000),@my_variable)
Sign up to request clarification or add additional context in comments.

1 Comment

I want to insert japanese into the table, which the value is from a CSV file. '@value' is declared as nvarchar to store the value from the csv file. But at this moment, if I insert '@value', still shows "???" in the db table. So I have to add something like N to '@value', then insert it into the table. Is there any way to achieve that? Or an alternative way?
0

Have you tried this?

declare @finalValue nvarchar(255);
set @finalValue=cast(@value as nvarchar(255))

also implicit conversion works as well.. may get truncation warning if you have a size mismatch.

declare @finalValue nvarchar(255);
set @finalValue=@value

3 Comments

NOT WORK. '@value' is already declared as nvarchar when it gets value from the CSV file.
If your @value is already set to Unicode and you're still getting "?" values... the source is not unicode to begin with. You'll have to convert from east asian charater set to unicode. In other words, the problem isn't value -> finalvalue but.. source -> value. I hope that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.