Unix Technical Forum

Determining Last Day of the Month--Quick UDF

This is a discussion on Determining Last Day of the Month--Quick UDF within the SQL Server Data Warehousing forums, part of the Microsoft SQL Server category; --> Hi All, Getting the last day of the month, for the given date, by TSQL is bit tricky. Following ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server Data Warehousing

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 07:08 PM
Namwar Rizvi
 
Posts: n/a
Default Determining Last Day of the Month--Quick UDF

Hi All,
Getting the last day of the month, for the given date, by TSQL is bit
tricky. Following user defined function provides you a handy general purpose
function to get the last day of the month.
It gets the last day of the month as follows:
For example we have a given date 17-March-2007

1.. Add a month in the given date by using DateAdd function. It will
become 17-April-2007

2.. Get the Day part of the date from the above step. It is 17

3.. Subtract number of days of the second step from the date of step 1. It
will become 31-March-2007
4.. Resulting date in step 3 is our answer.
Following is the TSQL code of the function

Create function
udf_GetLastDayOfTheMonth(@m_GivenDate datetime)
returns datetime
as
Begin
Return dateadd(day,-1*
day(dateadd(month,1,@m_GivenDate)),dateadd(month,1 ,@m_GivenDate))


End

Namwar

For many other utility functions and SQL Server information:

http://blog.namwarrizvi.com







Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 07:08 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Determining Last Day of the Month--Quick UDF

On Tue, 26 Jun 2007 23:47:24 +0100, Namwar Rizvi wrote:

(snip)

Hi Namwar,

Crossposting a message to seven groups is considered rude. Especially
when your message is not on-topic in any of them.

Even more to the point, your method of finding the last day of the month
is needlessly complex.

SELECT DATEADD(month,
DATEDIFF(month, '20000101', getdate()),
'20000131');

And don't enclose it in a UDF but inline it in your queries for best
performance.

>For many other utility functions and SQL Server information:
>
>http://blog.namwarrizvi.com


If you want to advertise your blog, at least make sure to check what you
write first, and to post to appropriate groups.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 07:08 PM
Adam Machanic
 
Posts: n/a
Default Re: Determining Last Day of the Month--Quick UDF

This has absolutely nothing to do with ANY of the groups you crossposted to.
Please refrain from needlessly spamming in these groups--SPAM masquerading
as a "tip" is still nothing more than SPAM. If you want to advertise, go
buy some Google ads.


--

Adam Machanic
SQL Server MVP

Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220



"Namwar Rizvi" <namwar@hotmail.com> wrote in message
news:ulEh4PEuHHA.3400@TK2MSFTNGP03.phx.gbl...
> Hi All,
> Getting the last day of the month, for the given date, by TSQL is bit
> tricky. Following user defined function provides you a handy general
> purpose function to get the last day of the month.
> It gets the last day of the month as follows:
> For example we have a given date 17-March-2007
>
> 1.. Add a month in the given date by using DateAdd function. It will
> become 17-April-2007
>
> 2.. Get the Day part of the date from the above step. It is 17
>
> 3.. Subtract number of days of the second step from the date of step 1.
> It will become 31-March-2007
> 4.. Resulting date in step 3 is our answer.
> Following is the TSQL code of the function
>
> Create function
> udf_GetLastDayOfTheMonth(@m_GivenDate datetime)
> returns datetime
> as
> Begin
> Return dateadd(day,-1*
> day(dateadd(month,1,@m_GivenDate)),dateadd(month,1 ,@m_GivenDate))
>
>
> End
>
> Namwar
>
> For many other utility functions and SQL Server information:
>
> http://blog.namwarrizvi.com
>
>
>
>
>
>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 07:08 PM
dba
 
Posts: n/a
Default Re: Determining Last Day of the Month--Quick UDF

On Jun 26, 4:03 pm, "Adam Machanic" <amacha...@IHATESPAMgmail.com>
wrote:
> This has absolutely nothing to do with ANY of the groups you crossposted to.
> Please refrain from needlessly spamming in these groups--SPAM masquerading
> as a "tip" is still nothing more than SPAM. If you want to advertise, go
> buy some Google ads.
>
> --
>
> Adam Machanic
> SQL Server MVP
>
> Author, "Expert SQL Server 2005 Development"http://www.apress.com/book/bookDisplay.html?bID=10220
>
> "Namwar Rizvi" <nam...@hotmail.com> wrote in message
>
> news:ulEh4PEuHHA.3400@TK2MSFTNGP03.phx.gbl...
>
>
>
> > Hi All,
> > Getting the last day of the month, for the given date, by TSQL is bit
> > tricky. Following user defined function provides you a handy general
> > purpose function to get the last day of the month.
> > It gets the last day of the month as follows:
> > For example we have a given date 17-March-2007

>
> > 1.. Add a month in the given date by using DateAdd function. It will
> > become 17-April-2007

>
> > 2.. Get the Day part of the date from the above step. It is 17

>
> > 3.. Subtract number of days of the second step from the date of step 1.
> > It will become 31-March-2007
> > 4.. Resulting date in step 3 is our answer.
> > Following is the TSQL code of the function

>
> > Create function
> > udf_GetLastDayOfTheMonth(@m_GivenDate datetime)
> > returns datetime
> > as
> > Begin
> > Return dateadd(day,-1*
> > day(dateadd(month,1,@m_GivenDate)),dateadd(month,1 ,@m_GivenDate))

>
> > End

>
> > Namwar

>
> > For many other utility functions and SQL Server information:

>
> >http://blog.namwarrizvi.com- Hide quoted text -

>
> - Show quoted text -


Is this what your looking for?:

declare
@date smalldatetime,
@day int,
@date_from smalldatetime,
@date_to smalldatetime

set @date = GetDate()

set @date = convert(smalldatetime, convert(varchar(10),@date,101))
set @day = datepart(dd, @date)
set @date_to = dateadd(dd, -@day, @date)
set @date_from = dateadd(m, -1, dateadd(dd, 1, @date_to))

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:11 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com