0

I need to get only date and hours from datetime field. How do I do that?

My query:

select  ImpFile, convert(nvarchar,ImpDate,21) as ImpDate
from nol_artikula_izmaina

The output from this query:
pic1
What I need is that it only shows me the date and the hour for example the field ImpDate should look like this: 2012-05-11 14:00:00:000 and 2012-05-11 16:00:00:000
Is that possible?

Brezhnews
  • 1,559
  • 2
  • 20
  • 37

2 Answers2

2

This works by getting the number of [whole] hours between "date 0" and ImpDate, then adding that to "date 0".

It's very similar in principle to removing the time (which gets the number of [whole] days) and can be used to "truncate" to any date part (though for seconds or smaller, you may need to use a different "floor" date than 0 to avoid an overflow).

 SELECT ImpFile, DATEADD(hh, DATEDIFF(hh, 0, ImpDate), 0)
 FROM nol_artikula_izmaina
Community
  • 1
  • 1
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
0

Select using datepart.

http://msdn.microsoft.com/en-us/library/aa258265(v=sql.80).aspx

Internet Engineer
  • 2,514
  • 8
  • 41
  • 54