[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

get the first of the month

rodchar

3/28/2007 1:01:00 AM

hey all,
i have a stored proc i'm working with:
i need to take the current date go back a certain number of months specified
in the parm.

for example:

SET @monthsDifference = DATEADD(month, -(monthsBack), GETDATE())

which takes us to:

12/26/2006

1. what i'd like to know is the easiest way to turn that date into the first
or last day of that month?

2. is there a way to modify the time of a date variable?

for example:

12/26/2006 3:49:01 PM

i'd like to set 3:49:01 to 12:00:00 AM

thanks,
rodchar


1 Answer

Alejandro Mesa

3/28/2007 1:07:00 AM

0

rodchar,

> 2. is there a way to modify the time of a date variable?
>
> for example:
>
> 12/26/2006 3:49:01 PM
>
> i'd like to set 3:49:01 to 12:00:00 AM

select
dateadd(day, datediff(day, 0, getdate()), 0),
cast( convert(char(8), getdate(), 112) as datetime)

> 1. what i'd like to know is the easiest way to turn that date into the first
> or last day of that month?

declare @d datetime

set @d = dateadd(day, datediff(day, 0, getdate()), 0)

select
cast(convert(varchar(6), @d, 112) + '01' as datetime) fdm,
dateadd(day, -day(dateadd(month, 1, @d)), dateadd(month, 1, @d)) ldm
go


AMB



"rodchar" wrote:

> hey all,
> i have a stored proc i'm working with:
> i need to take the current date go back a certain number of months specified
> in the parm.
>
> for example:
>
> SET @monthsDifference = DATEADD(month, -(monthsBack), GETDATE())
>
> which takes us to:
>
> 12/26/2006
>
> 1. what i'd like to know is the easiest way to turn that date into the first
> or last day of that month?
>
> 2. is there a way to modify the time of a date variable?
>
> for example:
>
> 12/26/2006 3:49:01 PM
>
> i'd like to set 3:49:01 to 12:00:00 AM
>
> thanks,
> rodchar
>
>