Explain Codes LogoExplain Codes Logo

Set an empty DateTime variable

csharp
nullable-types
datetime
sql-stored-procedures
Nikita BarsukovbyNikita Barsukov·Dec 21, 2024
TLDR

Set DateTime variable to NULL to display it's empty:

DECLARE @EmptyDate DATETIME = NULL;

If NULL isn't up your alley and you insist on a non-null default, then use SQL Server's minimum DateTime value of '1753-01-01':

DECLARE @DefaultDate DATETIME = '1753-01-01';

In scenarios where you're sharing a dance with a database and an unplanned date may occur (pun intended), nullable DateTime is your reliable partner in your C# code:

DateTime? freeSaturdayNight = null;

Deep dive into DateTime abyss

The nullable vs non-nullable showdown

In the Wild West of SQL and programming languages like C#, nullable types offer a clear white flag between an unset date and a minimum date value. In C#, DateTime is a value type and, therefore, cannot be null. However, you can use a nullable DateTime (DateTime? or Nullable<DateTime>), your trusty deputy, to represent the absence of value—like an empty whiskey bottle.

Wrangling nullable DateTime in C#

To set a nullable DateTime to an empty state in C#, assign it null like it's a wanted poster:

DateTime? tumbleWeedDate = null;

Before performing operations, always check for a value with HasValue to avoid shooting yourself in the foot with a null reference exception:

if (tumbleWeedDate.HasValue) { // Ride out with tumbleWeedDate.Value into the sunset }

Dancing with nullable DateTime in SQL Stored Procedures

When using nullable DateTime variables and you're handing them off to SQL Stored Procedures, you might need to give a null date a new look with DBNull.Value:

SqlParameter dateParameter = new SqlParameter("@Date", tumbleWeedDate.HasValue ? (object)tumbleWeedDate.Value : DBNull.Value);

This ensures your Stored Procedure doesn't throw a tantrum with the correct value, and can handle DBNull for optional dance dates.

Designing your DateTime safety net

Alternatives for non-nullable DateTime

For non-nullable DateTime, you may use DateTime.MinValue like an understudy waiting in the wings:

DateTime openingNight = DateTime.MinValue; // Or as C# veterans put it, new DateTime()

But be aware, DateTime.MinValue can be misleading. Much like actors forgetting their lines, it may not genuinely represent an absent date.

Avoiding DateTime pitfalls

Avoid falling off the stage by initializing DateTime variables to prevent drama... and exceptions. In C#, you can take these precautions:

DateTime openingNight = default; // The other name on the marquee: new DateTime()

This readies up your openingNight with the default value, which is DateTime.MinValue.

Engaging with NULL in SQL

The NULL in SQL often plays the part of absence. So, when juggling DateTime in databases, it's common to set a DateTime column as nullable and use NULL to represent a show that's yet to be scheduled.

CREATE TABLE Performances ( ShowID INT PRIMARY KEY, ShowDate DATETIME NULL );