Create Date in T-SQL - SQL Server

Here’s a very simple query that shows how to create a Date using T-SQL if you are given the Day, Month and Year

Create Date T-SQL

OUTPUT

image

If you are given only the Day and Month, then here’s a nice query written by Peter Larsson:

DECLARE
@Day smallint = 24,
@Month smallint = 4

SELECT DATEADD(MONTH, 12 * YEAR(GETDATE())
+ @Month - 22801, @Day - 1)
As [Date]

OUTPUT

image

No comments:

Post a Comment