Sunday, January 21, 2024

MS SQL deal with Date or Datetime

In C#, to create DateTime is easy. It could be the following.

DateTime datetime = new DateTime(2024, 1, 1);

After SQL Server 2016 (13.x), there is a new a function has been introduced which is DATEFROMPARTS.
We can declare the Date or DateTime more intuitively.

Date

SELECT DATEFROMPARTS( 2024, 1, 1) AS  Result;

DateTime

SELECT DATETIMEFROMPARTS( 2024, 1, 1, 0, 0, 0, 0) AS  Result;

It becomes more graceful.

No comments:

Post a Comment