- Introduction
- Date and Time UDFs
- DatePart
- TimePart
- GetWorkingDays
- AddWorkingDays
- FirstMonthDay
- LastMonthDay
Introduction
I would like to write the series of articles about useful User-Defined Functions grouped by the following categories:In this article, I wrote some useful Date and Time User-Defined Functions.
Date and Time UDFs
These scalar User-Defined Functions perform an operation on a date and time input value and return a string, numeric, or date and time value.DatePart
Returns the date part of the datetime value.Syntax
DatePart ( datepart )Arguments
datepart - datetime value.Return Types
varcharThe function's text:
CREATE FUNCTION dbo.DatePart |
Example
This example returns a character string contained the date part of the datetime value:SELECT dbo.DatePart('11/11/2000 11:15AM')
Here is the result set:
----------
11/11/2000
(1 row(s) affected)
TimePart
Returns the time part of the datetime value.Syntax
TimePart ( datepart )Arguments
datepart - datetime value.Return Types
varcharThe function's text:
CREATE FUNCTION dbo.TimePart |
Example
This example returns a character string contained the time part of the datetime value:SELECT dbo.TimePart('11/11/2000 11:15AM')
Here is the result set:
----------
11:15AM
(1 row(s) affected)
GetWorkingDays
Returns the number of working days between two dates (not including these dates).Syntax
GetWorkingDays ( StartDate, EndDate )Arguments
StartDate - the datetime value (start date).EndDate - the datetime value (end date).
Return Types
intThe function's text:
CREATE FUNCTION dbo.GetWorkingDays |
Example
Returns the number of working days between '11/13/2000' and '12/27/2000':SELECT dbo.GetWorkingDays ('11/13/2000', '12/27/2000')
Here is the result set:
-----------
31
(1 row(s) affected)
AddWorkingDays
Works like DATEADD, but adds the working days.Syntax
AddWorkingDays ( StartDate, WorkDays )Arguments
StartDate - the datetime value (start date).WorkDays - the integer value (number of working days).
Return Types
datetimeThe function's text:
CREATE FUNCTION dbo.ADDWorkingDays |
Example
Adds 9 working days to '11/13/2002' and returns the result date:SELECT dbo.AddWorkingDays ('11/13/2002', 9)
Here is the result set:
-------------------------------------
2002-11-26 00:00:00.000
(1 row(s) affected)
FirstMonthDay
Returns the first day of the month for the given date.Syntax
FirstMonthDay ( date )Arguments
date - datetime value.Return Types
datetimeThe function's text:
CREATE FUNCTION dbo.FirstMonthDay |
Example
Returns the first day for the '06/15/99' date:SELECT dbo.FirstMonthDay('06/15/99')
Here is the result set (from my machine):
-------------------------------------
1999-06-01 00:00:00.000
(1 row(s) affected)
LastMonthDay
Returns the last day of the month for the given date.Syntax
LastMonthDay ( date )Arguments
date - datetime value.Return Types
datetimeThe function's text:
CREATE FUNCTION dbo.LastMonthDay |
Example
Returns the last day for the '06/15/99' date:SELECT dbo.LastMonthDay('06/15/99')
Here is the result set (from my machine):
-------------------------------------
1999-06-30 00:00:00.000
(1 row(s) affected)
No comments:
Post a Comment