Nana Thompson Nana Thompson - 5 months ago 9
SQL Question

Time part of a DateTime Field in SQL

How would I be able to extract the time part of a DateTime field in SQL? For my project I have to return data that has a timestamp of 5pm of a DateTime field no matter what the date is

Answer

In SQL Server if you need only the hh:mi, you can use:

DECLARE @datetime datetime

SELECT @datetime = GETDATE()

SELECT CAST(DATEPART(hour, @datetime) as varchar(2)) + ':' +
       CAST(DATEPART(minute, @datetime)as varchar(2))