Date and Time User-Defined Functions | Database Journal

Date and Time User-Defined Functions

Jan 4, 2001
2 minute read




Introduction


Date and Time UDFs


  • DatePart

  • TimePart

  • GetWorkingDays

  • FirstMonthDay

  • LastMonthDay


  • Introduction

    I would like to write the series of articles about useful User-Defined
    Functions grouped by the following categories:


    • Date and Time User-Defined Functions

    • Mathematical User-Defined Functions

    • Metadata User-Defined Functions

    • Security User-Defined Functions

    • String User-Defined Functions

    • System User-Defined Functions

    • Text and Image User-Defined Functions


    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
    Is the datetime value.
    Return Types
    varchar
    The functions text:
    __ATLAS_TABLE_BLOCK__0__
    Examples
    This example returns a character string contained the date part
    of the datetime value:
    __ATLAS_TABLE_BLOCK__1__
    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
    Is the datetime value.
    Return Types
    varchar
    The functions text:
    __ATLAS_TABLE_BLOCK__2__
    Examples
    This example returns a character string contained the time part
    of the datetime value:
    __ATLAS_TABLE_BLOCK__3__
    Here is the result set:
    ———-
    11:15AM
    (1 row(s) affected)


    Advertisement

    GetWorkingDays

    
    Returns the number of working days between two dates
    (not including these dates).
    Syntax
    StrSeparate ( StartDate, EndDate )
    Arguments
    StartDate
    Is the datetime value (start date).
    EndDate
    Is the datetime value (end date).
    Return Types
    int
    The functions text:
    __ATLAS_TABLE_BLOCK__4__
    Examples
    Returns the number of working days between ’11/13/2000and12/27/2000′:
    __ATLAS_TABLE_BLOCK__5__
    Here is the result set:
    ———–
    31
    (1 row(s) affected)


    FirstMonthDay

    
    Returns the first day of the month for the given date.
    Syntax
    FirstMonthDay ( date )
    Arguments
    date
    Is the datetime value.
    Return Types
    datetime
    The functions text:
    __ATLAS_TABLE_BLOCK__6__
    Examples
    Returns the first day for the06/15/99date:
    __ATLAS_TABLE_BLOCK__7__
    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
    Is the datetime value.
    Return Types
    datetime
    The functions text:
    __ATLAS_TABLE_BLOCK__8__
    Examples
    Returns the last day for the06/15/99date:
    __ATLAS_TABLE_BLOCK__9__
    Here is the result set (from my machine):
    ——————————————————
    1999-06-30 00:00:00.000
    (1 row(s) affected)



    »


    See All Articles by ColumnistAlexander Chigrik

    Alexander Chigrik

    I am the owner of MSSQLCity.Com - a site dedicated to providing useful information for IT professionals using Microsoft SQL Server. This site contains SQL Server Articles, FAQs, Scripts, Tips and Test Exams.

    Database Journal Logo

    DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

    Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.