MS SQL: Get the Time portion from a DateTime

Microsoft SQL Server2 Comments on MS SQL: Get the Time portion from a DateTime

MS SQL: Get the Time portion from a DateTime

To get only the Time portion of a DateTime variable in Transact-SQL, you can use the following function:

-- Returns only the time portion of a DateTime, at the "base" date (1/1/1900)
CREATE function [dbo].[sudf_Common_TimeOnly]
(
      @dtDateTime datetime
)
returns datetime
as
begin
      -- Get the time only
    return dateadd(day, -datediff(day, 0, @dtDateTime), @dtDateTime)
end

Please notice that the base date is January 1st 1900. Any dates/times prior to this day won’t work,

Releated

Ulf Emsoy has long working experience in project management, software development and supply chain management.

2 thoughts on “MS SQL: Get the Time portion from a DateTime

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top