By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

Find the first Day (01) and the Last Day (28) for a specified month in SQL Function?

P: 4
I need to write an SQL Function (UDF) to find the First Day (int) and the Last Day (int) when I specify a date.

For example: When I specify (20130217) (i.e. Feb 17th, 2013), I should be able to find the First Day (01) and the Last Day (28) for the specified month (in this case, Feb).

How can I do that? Please provide the necessary code to implement this feature in my SQL Function.

Thanks.
Mar 4 '13 #1

✓ answered by ck9663

I think he's just asking for a straight forward function to get the first and last day of the month.


Here, find your dates here.

Happy Coding!!!


~~ CK

Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,430
This is not a code writing service but if you post the code you've tried along with a description of the problem you're having with the code, we can help guide you to a solution.

As a hint, you can use the various date functions combined with cast functions to build the dates you need.
Mar 4 '13 #2

ck9663
Expert 2.5K+
P: 2,878
I think he's just asking for a straight forward function to get the first and last day of the month.


Here, find your dates here.

Happy Coding!!!


~~ CK
Mar 4 '13 #3

P: 9
Expand|Select|Wrap|Line Numbers
  1. DECLARE @yourDate DATETIME
  2. SET @yourDate = '2013-02-17'
  3.  
  4. SELECT DAY(DATEADD(MONTH, DATEDIFF(MONTH, 0, @yourDate), 0)) AS First,
  5.        DAY(DATEADD(MONTH, DATEDIFF(MONTH, -1, @yourDate), -1)) AS Last
Dec 9 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.