MS SQL: Get the Date portion from a DateTime

Microsoft SQL Server4 Comments on MS SQL: Get the Date portion from a DateTime

MS SQL: Get the Date portion from a DateTime

To return only the date portion of a DateTime variable in Microsoft SQL server, you need to use a combination of the dateadd and datediff functions in Transact-SQL.

-- Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value.
CREATE  function [dbo].[sudf_Common_DateOnly]
(
    @dtDateTime DateTime
)
returns datetime
as
begin
    -- Get the date only
    return dateadd(dd, 0, datediff(dd, 0, @dtDateTime))
end

Releated

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

4 thoughts on “MS SQL: Get the Date portion from a DateTime

Leave a Reply

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

Back To Top


Subscribe to our newsletter