2015-10-29 46 views
0

我正在提供一个应该表示一个小时的单个整数。所以,如果它返回1,那么它就是凌晨1点,24小时的时钟等等,例如13是下午1点。我需要在SQL中将其转换为时间。将整数转换为时间

我知道在MYSQL他们有一个函数,做到这一点:

SEC_TO_TIME(TheHour * 60 * 60)

是否有一个相当于我可以在SQL中使用?我该怎么做呢?

+0

你需要返回哪些数据类型和格式? –

回答

1

你可以做这样的事情。

select cast(DATEADD(hour, 13, 0) as time) 

的好处是,它仍然会用负数或数值甚至工作在24

+0

谢谢你完美的作品。对于未来的人,如果我的变量是TheHour,则选择cast(DATEADD(小时,TheHour,0)作为时间) – vmedhe2

1

有两个T-SQL函数:

DATEFROMPARTS (year, month, day) 

TIMEFROMPARTS (hour, minute, seconds, fractions, precision) 

然后如果你需要格式化它,你可以使用CONVERT。

0
-- test data 
declare @hour_table table(hour_number int) 
while (select count(*) from @hour_table) < 24 
    begin 
     insert into @hour_table(hour_number) 
      select count(*) from @hour_table 
    end 

-- return results with your conversion to time string 
select 
    hour_number, 
    convert(varchar(8),timefromparts(hour_number, 0, 0, 0, 0),0) as time_string 
from @hour_table