Format SQL Server 2012 Time(7) to “HH:mm”

依然范特西╮ 提交于 2019-12-08 06:36:13

问题


I have Time(7) columns for each day in the week and I want to format it as HH:mm

I tried to use SQL Server 2012 new FORMAT function, but it only shows the NULL value.

How can I format a time(7) datatype to display as HH:mm?


回答1:


You should always format your data in the presentation layer. If there are circumstance where the TIME datatype cannot be formatted by your UI, you can cast the value to a DATETIME which will more than likely be supported.

SELECT CAST(TimeColumn AS DATETIME)
FROM MyTable



回答2:


You can use Format in Ms-sql To Get Format of Time as you Need

Example :

SELECT Format(GETDATE(), 'hh:mm')

Output :

07:52

For You Columns You can Replace GETDATE() with your column name as below -

Example :

SELECT Format(HourMonday, 'hh:mm')



回答3:


DECLARE @mytime Time(7)
set  @mytime=GETDATE()
select SUBSTRING(CONVERT(CHAR(50),CAST(@mytime as time)),1,5)



回答4:


Just to add a bit to the already chosen answer.

Since you are using Reporting Services you can do the formatting with Format():

=Format(Now(), "hh:mm")



回答5:


SELECT left(TimeColumn,5) as Time
FROM MyTable



回答6:


Format does work on time(7), but you have to escape the colon and hh mm are lower case.

Declare @HourMonday as  time(7) = '13:45:00'

Select FORMAT(@HourMonday, N'hh\:mm')


来源:https://stackoverflow.com/questions/26214052/format-sql-server-2012-time7-to-hhmm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!