Best data type for storing strings in SQL Server? Best data type for storing strings in SQL Server? sql-server sql-server

Best data type for storing strings in SQL Server?


nvarchar stores unicode character data which is required if you plan to store non-English names. If it's a web application, I highly recommend using nvarchar even if you don't plan on being international. The downside is that it consumes twice as much space, 16-bits per character for nvarchar and 8-bits per character for varchar.


What's the best data type to be usedwhen storing strings, like a firstname? I've seen varchar and nvarcharboth used. Which one is better? Doesit matter?

See What is the difference between nchar(10) and varchar(10) in MSSQL?

If you need non-ASCII characters, you have to use nchar/nvarchar. If you don't, then you may want to use char/varchar to save space.

Note that this issue is specific to MS SQL Server, which doesn't have good support for UTF-8. In other SQL implementations that do, you can use Unicode strings with no extra space requirements (for English).

EDIT: Since this answer was originally written, SQL Server 2019 (15.x) finally introduced UTF-8 support. You may want to consider using it as your default database text encoding.

I've also heard that the best lengthto use is 255, but I don't know why.

See Is there a good reason I see VARCHAR(255) used so often (as opposed to another length)?

Is there a specific length that ispreferred for strings?

If you data has a well-defined maximum limit (e.g., 17 characters for a VIN), then use that.

OTOH, if the limit is arbitrary, then choose a generous maximum size to avoid rejecting valid data. In SQL Server, you may want to consider the 900-byte maximum size of index keys.


nvarchar means you can save unicode character inside it. there is 2GB limit for nvarchar type. if the field length is more than 4000 characters, an overflow page is used. smaller fields means one page can hold more rows which increase the query performance.