7

I have table in SQL Server say "Temp" and it has Addr1, Addr2, Addr3, Addr4 columns and some additional columns also there.

These Addr1, Addr2, Addr3 and Addr4 are nvarchar type. when I check the size of this column by object explorer. it shows all of them in nvarchar(100).

enter image description here

But when I check them using Alt + F1. It shows the details in Result Pane with the length as 200. screenshot is below.

enter image description here

why there is different? If I enter more than 100 characters, I'm getting truncation errors? seems like it taking only 100 characters.

can you please let me know what is the length value specifies ?

Thanks, Prakash.

ps_prakash02
  • 543
  • 4
  • 18
  • [already anwered here](http://stackoverflow.com/a/21259797/4366471). – Wojciech Tomczyk Jan 05 '17 at 12:31
  • 1
    @WojciechTomczyk is it? That answer doesn't seem to deal specifically with the difference between what you see in Object Explorer versus the results of `sp_help`, just states that `NVARCHAR` values take twice as much storage space. Unless I'm misunderstanding the question/answer you've linked to. – 3N1GM4 Jan 05 '17 at 12:39
  • @3N1GM4 Yes, +1 for that. I'm too enthusiastic in linking answers... – Wojciech Tomczyk Jan 05 '17 at 12:55

1 Answers1

14

Because the size listed in Object Explorer is number of characters and the size listed in the result of your query to sp_help is number of bytes.

VARCHAR values in SQL use 1 byte per character, whereas NVARCHAR values use 2 bytes per character. Both also need a 2 byte overhead - see below. So because you are looking at NVARCHAR columns, these need 200 (well actually 202) bytes to store 100 characters, where a VARCHAR would only require 100 (really 102).

References:

MSDN: char and varchar

The storage size is the actual length of the data entered + 2 bytes.

MSDN: nchar and nvarchar:

The storage size, in bytes, is two times the actual length of data entered + 2 bytes.

(emphasis mine)

MSDN: sp_help:

Reports information about a database object (any object listed in the sys.sysobjects compatibility view), a user-defined data type, or a data type.

    /------------------------------------------------------------------------\
    | Column name | Data type | Description                                  |
    |-------------+-----------+----------------------------------------------|
    |   Length    | smallint  | Physical length of the data type (in bytes). |
    \------------------------------------------------------------------------/
3N1GM4
  • 3,372
  • 3
  • 19
  • 40