问题
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