Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Date Question in SQL

Status
Not open for further replies.

azaveri

IS-IT--Management
Mar 25, 2010
14
US
What is the syntax for specifying a date to a variable without the year? For example, I have a parameter of @YEAR and I want the @STARTDATE variable to always be '1/1/@YEAR'.

Thanks
 
If I understand your question correctly...

Code:
Declare @Year Int

Set @Year = 2009

Declare @StartDate DateTime

Set @StartDate = DateAdd(Year, @Year-1900, '1900-01-01')

Select @StartDate

If you subtract 1900 from your year and then add that number of years to January 1, 1900, you will end up with a date that is January 1 of whatever year you want.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
how about

Declare @Year Varchar(5)
Set @Year = 2009
Declare @StartDate DateTime
Set @StartDate = Convert(Datetime,'1/1/'+@year)
Select @StartDate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top