Monday, March 12, 2012

Last day of month

If I keep a hard sql statment to show all records from 1 to 31 of each month
will get error on months where there are no 31 days, how can i know the last
day of the month ? any sql function or just via programming language?
Thanks!
A quick & dirty way is to find any date of the subsequent month and do:
SELECT @.dt - DAY ( @.dt )
where @.dt is a any date of the month following the one in question. It is
better to use DATEADD function rather than arithmetic operations like +
nd - when dealing with DATETIME datatypes, but used here only for
illustration purposes.
Anith
|||May I recommend doing an online search for your question prior to posting?
:-) I did a search of this collection of words and got several examples of
code I could use: sql last day of month function
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net
"Paulo" <prbspfc@.uol.com.br> wrote in message
news:Ok88SnHVIHA.748@.TK2MSFTNGP04.phx.gbl...
> If I keep a hard sql statment to show all records from 1 to 31 of each
> month will get error on months where there are no 31 days, how can i know
> the last day of the month ? any sql function or just via programming
> language?
> Thanks!
>
|||I did it via ASP, found on google!
Private Function GetMonthEnd(Month,Year)
GetMonthEnd = DateAdd("d", -1, DateAdd("m", 1, ((Year & "/" & Month &
"/01"))))
End Function
Thanks to all!
"TheSQLGuru" <kgboles@.earthlink.net> escreveu na mensagem
news:13ofcfkjfpv34b3@.corp.supernews.com...
> May I recommend doing an online search for your question prior to
> posting? :-) I did a search of this collection of words and got several
> examples of code I could use: sql last day of month function
> --
> Kevin G. Boles
> Indicium Resources, Inc.
> SQL Server MVP
> kgboles a earthlink dt net
>
> "Paulo" <prbspfc@.uol.com.br> wrote in message
> news:Ok88SnHVIHA.748@.TK2MSFTNGP04.phx.gbl...
>

No comments:

Post a Comment