date vs datetime sql server code example

Example 1: sql server datetime vs datetime2

The MSDN documentation for datetime recommends using datetime2. Here is their recommendation:

Use the time, date, datetime2 and datetimeoffset data types for new work. These types align with the SQL Standard. They are more portable. time, datetime2 and datetimeoffset provide more seconds precision. datetimeoffset provides time zone support for globally deployed applications.

datetime2 has larger date range, a larger default fractional precision, and optional user-specified precision. Also depending on the user-specified precision it may use less storage.

Example 2: date vs datetime

DATE : YYYY-MM-DD format
DATETIME : YYYY-MM-DD HH:MM:SS format
TIMESTAMP : X s from 1970 format
TIME : HH:MM:SS format

Tags:

Sql Example