Determining Nvarchar length

情到浓时终转凉″ 提交于 2019-12-10 02:33:49

问题


I've read all about varchar versus nvarchar. But I didn't see an answer to what I think is a simple question. How do you determine the length of your nvarchar column? For varchar it's very simple: my Description, for example, can have 100 characters, so I define varchar(100). Now I'm told we need to internationalize and support any language. Does this mean I need to change my Description column to nvarchar(200), i.e. simply double the length? (And I'm ignoring all the other issues that are involved with internationalization for the moment.)

Is it that simple?


回答1:


Generally it is the same as for varchar really. The number is still the maximum number of characters not the data length.

nvarchar(100) allows 100 characters (which would potentially consume 200 bytes in SQL Server).

You might want to allow for the fact that different cultures may take more characters to express the same thing though.

An exception to this is however is if you are using an SC collation (which supports supplementary characters). In that case a single character can potentially take up to 4 bytes.

So worst case would be to double the character value declared.




回答2:


You can get the answer with a simple experiment, by executing the code below.

DECLARE @a varchar(4);  SET @a = '123456';  SELECT @a; 
DECLARE @b nvarchar(4);  SET @b = '123456';  SELECT @b;  

They both will return your result '1234' only store 4 characters.

How do you determine the length of your nvarchar column? Just put the string number of characters you wanted to store in that column.



来源:https://stackoverflow.com/questions/21259726/determining-nvarchar-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!