如何在SQL中将bigint转换为小时和分钟?

问题描述 投票:0回答:2

我有以下问题:我有一个带有BIGINT列的数据库,并且我试图将其转换为一个小时,所以我的数据库看起来像这样

id minutes hours and minutes 
------------------------------
1   400       null
2   450       null
3   421       null
4   140       null

我需要将保存为bigint的分钟数转换为小时数和分钟数;我怎样才能做到最好?

我需要这个结果

 id minutes hours and minutes 
 ----------------------------
    1   400       06:40
    2   450       07:30
    3   421       null
    4   140       null

我是Google的初学者,但是我找不到我的问题的答案,有人可以帮助我还是给我链接-我该怎么做?谢谢

sql timestamp converters bigint
2个回答
2
投票

您可以使用算术来计算小时和分钟:

select floor(minutes_col / 60) as hours,
       minutes_col % 60 as minutes

某些数据库使用mod()作为模函数。

转换为字符串在很大程度上取决于数据库。


0
投票

SQL具有专门用于节省时间的数据类型:time(n) ((n可以在0-7之间) Check here for more info about this datatype

您的第三列应该是这种类型。然后您可以从分钟列值中计算小时和分钟,如下所示:

小时=总分钟数/ 60,分钟=总分钟数%60。然后在第三栏中保存“小时:分钟”。

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.