SQL Server – Best Smallest Data Type Size for 19 Digit Number

datatypessql serversql-server-2017timestamp

I'm using this code to get a mix of Date and Time to use it like a unique ID

DateTime.Now.ToString("yyyyMMddHHmmssfffff")

In SQL Server 2017 I'm confused as to which best smallest size of data type would be that I can use to store this string.

What is the best smallest data type: bigint, timestamp or varchar(19)?

Best Answer

For sure you don't want to use a varchar: you are trying to store a number, so storing it into a string is a non sense. Plus, regardless of what you are going to do it later, varchar is the field that allows you the less flexibility: really no point in using it.

Your want to avoid timestamp, too, as it is deprecated and anyway it's intended for a different use (it's just a synonym for rowversion)

Your choice is then between bigint and datetime2(5), but given your requested precision of 5 they will both use 8 bytes, so no clear winner here. The only edge that datetime2 may have is that being a datetime it's better handled by datetime functions. Bigint, instead, will allow you to represent negative years (in case you need it).