Combine DateADD and DateDIFF to get the last date of a month : DATEDIFF « Date Timezone « SQL Server / T-SQL

SQL Server / T-SQL
1. Aggregate Functions
2. Analytical Functions
3. Constraints
4. Cursor
5. Data Set
6. Data Type
7. Database
8. Date Timezone
9. Index
10. Insert Delete Update
11. Math Functions
12. Select Query
13. Sequence
14. Store Procedure Function
15. String Functions
16. Subquery
17. System
18. Table
19. Table Joins
20. Transact SQL
21. Transaction
22. Trigger
23. View
24. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL » Date Timezone » DATEDIFF 
Combine DateADD and DateDIFF to get the last date of a month


23>
24> -- Creating a User-Defined Function
25>
26/*
27~   Returns a date representing the last date
28~   of any given month.
29~ */
30CREATE Function dbo.fn_LastOfMonth(@TheDate DateTime)
31> Returns DateTime
32> AS
33BEGIN
34>   DECLARE @FirstOfMonth  DateTime
35>   DECLARE @DaysInMonth Int
36>   DECLARE @RetDate DateTime
37>
38>   SET @FirstOfMonth = DATEADD(mm, DATEDIFF(mm,0,@TheDate)0)
39>   SET @DaysInMonth = DATEDIFF(d, @FirstOfMonth, DATEADD(m, 1, @FirstOfMonth))
40>   RETURN  DATEADD(d, @DaysInMonth - 1, @FirstOfMonth)
41END
42> GO
1select dbo.fn_LastOfMonthgetdate() )
2> GO

-----------------------
2006-10-31 00:00:00.000

(rows affected)
1> drop function dbo.fn_LastOfMonth;
2> GO
1>
2>
           
       
Related examples in the same category
1. DATEDIFF: return the difference between two dates
2. select DATEDIFF(second, '06:46:45', '11:35:00')
3. select DATEDIFF(quarter, '2001-12-01', '2002-09-30')
4. Find the number of days between 24th March 2001 and 24th March 1964
5. DATEDIFF(yyyy, "24 March 2001","24 March 1964")
6. DATEDIFF(mi, "24 March 2001","24 March 1964")
7. DATEDIFF(wk, "24 March 2001","24 March 1964")
8. DATEDIFF(m, "24 March 2001","24 March 1964")
9. DATEDIFF(d, @thisdate, @nextmonth) 'Days between'
10. DATEDIFF(month, '9-8-1989', '10-17-1991')
11. DATEDIFF(day, '9-8-1989', '10-17-1991')
12. Difference in weeks between 7-2-1996 and 8-4-1997
13. DATEDIFF(ss, @MyBirthDate, GETDATE())
14. select DATEDIFF(hour, '06:46:45', '11:35:00')
15. select DATEDIFF(minute, '06:46:45', '11:35:00')
16. Difference between the current time and UTC
17. DATEDIFF: date difference
18. Passing a column name to the DATEDIFF() value argument
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.