-3

How to get the date and hour information for a given datetime object in Transact-SQL?

E.g. 2014-12-18 21:00:00 for 2014-12-18 21:24:05.

I need to truncate off all parts after the hour - i.e. minutes, seconds, and partial seconds.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
dingx
  • 1,621
  • 3
  • 20
  • 38
  • 1
    Have you tried anything yourself? If so, show what you tried. If not, please do so before asking for someone here to do it for you. – alroc Dec 18 '14 at 13:48

2 Answers2

1

For SQL Server:

Select DATEPART(HOUR, [date]) + ":" + DATEPART(MINUTE, [date]);

Oracle:

Select TO_CHAR([date], 'HH:MI') From...
AHiggins
  • 7,029
  • 6
  • 36
  • 54
bowlturner
  • 1,968
  • 4
  • 23
  • 35
0
SELECT
CONVERT(TIME,GETDATE()) AS TimeOnly,
CONVERT(DATE,GETDATE(),101) AS DateOnly
GO

Replace the GETDATE() function with whatever you need.

daZza
  • 1,669
  • 1
  • 29
  • 51