Nana Thompson Nana Thompson - 1 year ago 40
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


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))