Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

How can I insert a unicode character through stored procedue variable?

I have a table with an nvarchar(4000) column and i need to store speech phonemes which are unicode characters in the table through a stored procedure. I know I need to place and N' before the string being passed in. My stored procedure parameter is also set to be a varchar(8000) data type and this cannot be changed.

I have tried: set @Value ='N' + cast(@value as nvarchar(4000))  and set @Value ='N'''+ cast(@value as nvarchar(4000))  + ''''
where @value is '/Q/, /ð/'

The problem is the table shows me  N'/?/, /ð/' or N/?/, /ð/ which is not the correct characters and i do not want to store 'N' in my text column.

What am I doing wrong?

2 Solutions

set @Value =N'' + cast(@value as nvarchar(4000)) +''
I may be mistaken, but if the input parameter is VARCHAR, I do not think there is any way you can pass in unicode data. SQL Server will automatically convert it to VARCHAR as it comes into the procedure, and it cannot be cast back to unicode. Using the N'' notation is a way to, within T-SQL, to tell SQL server that the string is unicode, but by just 'computing'  N'' around a VARCHAR string does not convert it to unicode. The results you are getting would be expected, because you are saying: concatenate the literal N (or N'), my string, converted to unicode, and ' together. Once a unicode character has been converted to ASCII, you have lost the information necessary to convert it back to unicode.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now