SQL Server 2008查询中的分钟到hh:mm格式(Minutes to hh:mm format in SQL Server 2008 query)

编程入门 行业动态 更新时间:2024-10-16 18:38:49
SQL Server 2008查询中的分钟到hh:mm格式(Minutes to hh:mm format in SQL Server 2008 query)

我的表中有一列Time 。 它保持一个以分钟为单位的时间值,即:605。我想把它显示为10:05,如hh:mm格式。

我是这样做的:

... Time = cast(AVG(Time) / 60 as varchar) + ':' + cast(AVG(Time) % 60 as varchar), ...

它将值显示为10:5,但我想将其设为10:05,并且我找不到在分钟部分中在'5'之前添加'0'的方法。

任何帮助,将不胜感激。

I have a column Time in my table. It holds a time value in minutes, ie: 605. What I want it to show it as 10:05, as hh:mm format.

I am doing it like this:

... Time = cast(AVG(Time) / 60 as varchar) + ':' + cast(AVG(Time) % 60 as varchar), ...

It shows the value as 10:5, but I want to make it as 10:05 and I couldn't find out a way to add '0' before '5' in the minutes section.

Any help would be appreciated.

最满意答案

您可以使用case表达式来执行此操作。

此外,您应该始终在使用强制转换时指定varchar的长度,或者它只显示第一个字符。

case when len(cast(AVG(Time)%60 as varchar(2))) < 2 then '0'+cast(AVG(Time)%60 as varchar(2)) else cast(AVG(Time)%60 as varchar(2)) end

You can use a case expression to do this.

Also you should always specify the length of varchar when you use cast, or it would only show the first character.

case when len(cast(AVG(Time)%60 as varchar(2))) < 2 then '0'+cast(AVG(Time)%60 as varchar(2)) else cast(AVG(Time)%60 as varchar(2)) end

更多推荐

本文发布于:2023-08-06 17:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1452786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   hh   SQL   Server   format

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!