SSRS 2008 - 如何将时间字段舍入到最接近的秒

时间:2011-03-17 20:08:29

标签: sql-server-2008 ssrs-2008

SSRS 2008

将时间字段四舍五入到最接近的秒的最简单方法是什么,这样我就没有时间格式,如00:00:39.190000,而是00:00:39?

我尝试过“t”格式,这种格式是短时间格式,但似乎没有用。

2 个答案:

答案 0 :(得分:2)

如果要将其格式化为字符串,请使用CONVERT。如果要实际截断日期时间的小数部分,请使用DATEADD将其减去。

declare @now datetime;
declare @floored datetime;
set @now=GETDATE();
-- get rid of the factional part by subtracting it.
-- datetime has worse than 1 msec of precision so this is exact.
-- datetime2 has 100 nsec precision; will need to use ns then.
set @floored = DATEADD(MS,-DATEPART(ms,@now),@now); 
-- output as value and as string.
-- see documentation on date and time styles. I like ODBC (120/121).
select
@now as GetDateTime,
@floored as GetDateFloored,
CONVERT(varchar,@now,120) as GetDateString, 
CONVERT(varchar,@floored,120) as GetDateFlooredAsString 

答案 1 :(得分:1)

如果您使用这些属性,则可以输入您想要的任何格式字符串。

如果值占用完整单元格,则应为文本框设置格式,否则占位符和最后一个最差选项是使用公式。 (=格式(Fields.DetailedTime.Value,“HH:mm:ss”))

将格式设置为hh:mm:ss http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=VS.80%29.aspx

标准的.net格式字符串在这里工作: http://msdn.microsoft.com/en-us/library/fbxft59x%28v=VS.80%29.aspx