Home All Groups Group Topic Archive Search About

set datetime month from int

Author
20 Jul 2006 9:11 PM
randy1200
The problem with the code below is that it ADDS the value of @year to the
current @thedate value. I need to SET @thedate.year to the @year value. Any
suggestions?

Thanks,
Randy


declare @month int
set @month = 3
declare @year int
set @year = 1999

declare @thedate datetime
set @thedate = GetDate()
set @thedate = dateadd(yy, @year, @thedate)
print @thedate
--
Randy

Author
20 Jul 2006 9:19 PM
Chris Lim
randy1200 wrote:
> The problem with the code below is that it ADDS the value of @year to the
> current @thedate value. I need to SET @thedate.year to the @year value. Any
> suggestions?

Try this:

DECLARE @month INT
SET @month = 3
DECLARE @year INT
SET @year = 1999


DECLARE @thedate DATETIME
SET @thedate = GETDATE()
SET @thedate = dateadd(
             yy,
             datediff(yy, '1/1/' + CONVERT(VARCHAR, @year), @thedate) *
-1,
             @thedate)
PRINT @thedate
Author
20 Jul 2006 9:25 PM
Tom Cooper
declare @month int
set @month = 3
declare @year int
set @year = 1999

declare @thedate datetime
set @thedate = GetDate()
set @thedate = dateadd(yy, @year - Year(@thedate), @thedate)
print @thedate

Tom

Show quote
"randy1200" <randy1200@newsgroups.nospam> wrote in message
news:DCD1844B-DD6F-4C5C-98A1-56FB4D39CC8A@microsoft.com...
> The problem with the code below is that it ADDS the value of @year to the
> current @thedate value. I need to SET @thedate.year to the @year value.
> Any
> suggestions?
>
> Thanks,
> Randy
>
>
> declare @month int
> set @month = 3
> declare @year int
> set @year = 1999
>
> declare @thedate datetime
> set @thedate = GetDate()
> set @thedate = dateadd(yy, @year, @thedate)
> print @thedate
> --
> Randy
Author
21 Jul 2006 12:01 PM
randy1200
Great responses! Many thanks...
--
Randy


Show quote
"randy1200" wrote:

> The problem with the code below is that it ADDS the value of @year to the
> current @thedate value. I need to SET @thedate.year to the @year value. Any
> suggestions?
>
> Thanks,
> Randy
>
>
> declare @month int
> set @month = 3
> declare @year int
> set @year = 1999
>
> declare @thedate datetime
> set @thedate = GetDate()
> set @thedate = dateadd(yy, @year, @thedate)
> print @thedate
> --
> Randy

AddThis Social Bookmark Button