SQL Server Admin
T-SQL Articles

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


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

0 Responses to "Create Date in T-SQL - SQL Server"
 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions