65.9K
CodeProject is changing. Read more.
Home

Removing the minutes and seconds from a DATETIME

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (6 votes)

Mar 25, 2020

CPOL
viewsIcon

16039

A basic user defined function to remove minute and second from a DATETIME

Using the Code

Open a new query, and paste the code:

-- =============================================
-- Author:        OriginalGriff (SM/PG)
-- Create date: 2020-03-25
-- Description:    Remove minutes and seconds from a datetime
-- =============================================
CREATE FUNCTION fnStripMinSec 
(
    @DT DATETIME
)
RETURNS DATETIME
AS
BEGIN
    RETURN DATEADD(hour, DATEDIFF(hour, 0, @DT), 0);
END
GO

This creates a simple user defined fucntion you can call at any point in your query:

SELECT GETDATE() AS [Now], dbo.fnStripMinSec(GETDATE()) AS [Stripped];

Will give you:

Now                        Stripped
2020-03-25 09:36:27.960    2020-03-25 09:00:00.000

History

  • 2020-03-25: First version