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,
2 thoughts on “MS SQL: Get the Time portion from a DateTime”