Given:
DECLARE @.AnalysisMonth int
DECLARE @.EndingDate int
SET @.AnalysisMonth = 1
How do I set the @.EndingDate variable to 31, i.e. the first month (1,
January) has 31 days?Starting with a date to get the last day of the month:
declare @.d datetime
set @.d = '29 Jan 2006'
select dateadd(day,-datepart(day, dateadd(month,1,@.d)),
dateadd(month,1,@.d))
2006-01-31 00:00:00.000
What this does is jump ahead one month, then back as many days as that
day of the month.
Roy Harvey
Beacon Falls, CT
On Tue, 28 Feb 2006 17:50:06 -0700, "Terri" <terri@.cybernets.com>
wrote:
>Given:
>DECLARE @.AnalysisMonth int
>DECLARE @.EndingDate int
>SET @.AnalysisMonth = 1
>How do I set the @.EndingDate variable to 31, i.e. the first month (1,
>January) has 31 days?|||Just start with the first day of the month and subtract a day and see what
it is.|||Terri
SELECT DATEADD(month,DATEDIFF(month,'19000101',
GETDATE()),'19000101')-1
"Terri" <terri@.cybernets.com> wrote in message
news:du2r3v$8sv$1@.reader2.nmix.net...
> Given:
> DECLARE @.AnalysisMonth int
> DECLARE @.EndingDate int
> SET @.AnalysisMonth = 1
> How do I set the @.EndingDate variable to 31, i.e. the first month (1,
> January) has 31 days?
>
>
>
>|||I like this.
malcolm
"Roy Harvey" wrote:
> Starting with a date to get the last day of the month:
> declare @.d datetime
> set @.d = '29 Jan 2006'
> select dateadd(day,-datepart(day, dateadd(month,1,@.d)),
> dateadd(month,1,@.d))
> --
> 2006-01-31 00:00:00.000
> What this does is jump ahead one month, then back as many days as that
> day of the month.
> Roy Harvey
> Beacon Falls, CT
> On Tue, 28 Feb 2006 17:50:06 -0700, "Terri" <terri@.cybernets.com>
> wrote:
>
>
Tuesday, February 14, 2012
Calculating last date of the month
Labels:
1how,
analysismonth,
calculating,
database,
date,
endingdate,
givendeclare,
intdeclare,
intset,
microsoft,
mysql,
oracle,
server,
sql,
variable
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment