Search code examples
sql-serverurlcolumn-types

What is the best column type for URL?


What is the best column type for a URL field for SQL Server?

Type: VARCHAR or NVARCHAR?

Length?

Similar question for MySQL.


Solution

  • If you are prepared to always URL encode your URLs before you store them (an example turned up by Google was 中.doc URL encoding to %E4%B8%AD.doc) then you are safe sticking with varchar. If you want the non-ASCII characters in your URLs to remain readable in the database then I'd recommend nvarchar. If you don't want to be caught out, then go for nvarchar.

    Since IE (the most restrictive of the mainstream browsers) doesn't support URLs longer than 2083 characters, then (apart from any considerations you might have on indexing or row length), you can cover most useful scenarios with nvarchar(2083).