datetime vs datetimeoffset in SQL Server: What’s the Difference?

This article highlights the main differences between the datetime and datetimeoffset data types in SQL Server.

Both data types are used for storing date and time values. But there are significant differences between the two.

Perhaps the most obvious difference is that the datetimeoffset stores the time zone offset, whereas datetime doesn’t.

Another important difference is that datetimeoffset allows you to specify the precision (up to 7 decimal places). This means that datetimeoffset values can vary in their storage size, depending on the precision being used.

The datetime type on the other hand, has a fixed storage size and precision.

Continue reading

datetime2 vs datetimeoffset in SQL Server: What’s the Difference?

This article looks at the main differences between the datetime2 and datetimeoffset data types in SQL Server.

Both data types are used for storing date and time values. Both are very similar, but with one key difference; the datetimeoffset stores the time zone offset.

This also results in datetimeoffset using more storage space than datetime2, so you would only use datetimeoffset if you need the time zone offset.

Continue reading

datetime vs smalldatetime in SQL Server: What’s the Difference?

This article explores the main differences between the datetime and smalldatetime data types in SQL Server.

Both data types are used for storing date and time values, however, there are differences between the two. In most cases you’re better off avoiding both types and using datetime2 instead (Microsoft also recommends this). In any case, here’s a comparison of these two data types.

Continue reading

datetime2 vs smalldatetime in SQL Server: What’s the Difference?

This article explores the main differences between the datetime2 and smalldatetime data types in SQL Server.

Both data types are used for storing date and time values, however, there are some important differences between the two. In most cases you’re better off using datetime2 (Microsoft also recommends this), however there might be some scenarios where you need to use smalldatetime.

Continue reading

Understanding ‘time’ Storage Size in SQL Server

In this article I look at the storage size of the time data type in SQL Server.

In particular, I look at the following:

  • Microsoft’s documentation
  • Data stored in a variable
    • Length in bytes using DATALENGTH()
    • Length in bytes using DATALENGTH() after converting to varbinary
  • Data stored in a database
    • Length in bytes using COL_LENGTH()
    • Length in bytes using DBCC PAGE()

Continue reading

Understanding ‘datetimeoffset’ Storage Size in SQL Server

In this article I look at how the datetimeoffset data type is stored in SQL Server, and how you can get different reported storage size results, depending on what you’re doing with it.

This is similar to what I did with the datetime2 data type.

In particular, I look at the following:

  • Microsoft’s documentation
  • Data stored in a variable
    • Length in bytes using DATALENGTH()
    • Length in bytes using DATALENGTH() after converting to varbinary
  • Data stored in a database
    • Length in bytes using COL_LENGTH()
    • Length in bytes using DBCC PAGE()

Continue reading

Understanding ‘datetime2’ Storage Size in SQL Server

In this article I share some observations I’ve had regarding the datetime2 data type’s storage size in SQL Server. Perhaps I will clarify some points about the actual storage size used by this data type when stored in a database.

In particular, I look at the following:

  • Microsoft’s documentation
  • Data stored in a variable
    • Length in bytes using DATALENGTH()
    • Length in bytes using DATALENGTH() after converting to varbinary
  • Data stored in a database
    • Length in bytes using COL_LENGTH()
    • Length in bytes using DBCC PAGE()

Some of those seem to contradict each other, and you will see two different storage size amounts for the same value, depending on where you look.

Continue reading

Converting Between Date & Time Data Types in SQL Server (T-SQL Examples)

When you convert between date and time data types in SQL Server, you need to be mindful of how the new data type will handle the value you’re trying to assign to it.

In some cases you might lose part of the value, in other cases you might gain a bunch of zeros (increasing storage size in the process). You may also end up with a value that’s been rounded up.

The following articles contain examples of conversions between the different date and time data types, with a particular focus on the issues I just mentioned.

Continue reading

Convert ‘datetimeoffset’ to ‘time’ in SQL Server (T-SQL Examples)

If you have a datetimeoffset value, but you don’t need the date and time zone offset part, converting it to time will save you a lot of storage space (while removing unnecessary details from the value). This article contains examples of converting a datetimeoffset value to a time value in SQL Server.

Continue reading