On SQL Server 2005 i want to create a represe开发者_StackOverflow中文版ntation of the hour that my task was started. I have a datetime that is '2010-10-01 12:30:00.000' which is the actual start time. What i would like to end up with is '12:00' which is the hour the task was started. Any good ideas on how to loose the '30' minutes?
declare @dt datetime
set @dt = '2010-10-01 12:30:00.000'
select convert(char(5), dateadd(hour, datediff(hour, 0, @dt), 0), 108)
Result:
12:00
As a datetime
declare @dt datetime
set @dt = '2010-10-01 12:30:00.000'
select dateadd(hour, datediff(hour, 0, @dt), 0)
Result:
2010-10-01 12:00:00.000
If you're just wanting to eliminate minutes/seconds/mills from a datetime value, but keep it as a datetime, use DATEADD
/DATEDIFF
:
select DATEADD(hour,DATEDIFF(hour,0,`2010-10-01T12:30:00`),0)
Which will return 2010-10-01T12:00:00
.
This will do what you want:
SELECT DATEADD(hh, DATEDIFF(hh, '20000101', [DateField]), '20000101')
精彩评论