Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Transoforming a Month & Year into a date

Author  Topic 

Nick
Posting Yak Master

155 Posts

Posted - 2001-12-06 : 09:58:25
I have a stored procedure that gets passed the month and year that a certain operation happened. One of my statements in the SP needs to insert a date. I will just be using the first day of the month for the insert. Is there an easy way to transform my month (an int) and year into a valid date? Thanks!

Nick
Posting Yak Master

155 Posts

Posted - 2001-12-06 : 11:04:22
Here is what I did, and it seemed to work:


DECLARE @datePosted datetime
SET @datePosted=@monthPosted + '/1/' + @yearPosted


quote:

I have a stored procedure that gets passed the month and year that a certain operation happened. One of my statements in the SP needs to insert a date. I will just be using the first day of the month for the insert. Is there an easy way to transform my month (an int) and year into a valid date? Thanks!





Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2001-12-06 : 13:01:56
select @datePosted = convert(datetime(convert(char(4),@yearPosted) + convert(char(4),@monthPosted) + '01')

or
select @datePosted = dateadd(mm,@monthPosted,dateadd(yy,@yearPosted,'19000101'))




==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Nick
Posting Yak Master

155 Posts

Posted - 2001-12-06 : 13:23:23
I needed to subtract 1 and 1900 from the monthPosted and yearPosted respectively, but thanks for getting me started!

Nick

quote:

select @datePosted = convert(datetime(convert(char(4),@yearPosted) + convert(char(4),@monthPosted) + '01')

or
select @datePosted = dateadd(mm,@monthPosted,dateadd(yy,@yearPosted,'19000101'))




==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.



Go to Top of Page
   

- Advertisement -