Nana Thompson Nana Thompson -5 years ago 107
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 Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download