开发者

SQL Server 2005 i want to create the hour a task was started out of an datetime

开发者 https://www.devze.com 2023-04-07 18:07 出处:网络
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 tim

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

精彩评论

暂无评论...
验证码 换一张
取 消