January 09, 2011

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


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

No comments: