在我的表中,我有两个time(2)
类型的字段:startTime
和endTime
。
SELECT startTime, endTime FROM ...
给出如下格式的时间:13:00:00
。我想显示时间在我的应用程序没有秒,例如13:00
。我尝试:SELECT FORMAT(startTime, 'hh:mm') as startTime FROM ...
,但当我执行它时,我得到空值:
startTime
_____________
NULL
NULL
这很奇怪,因为如果我正常选择我的数据,像这样SELECT startTime FROM ...
,我得到:
startTime
___________
12:00:00
13:00:00
我做错了什么?
而不是格式()糟糕的性能,考虑convert(...)
或者甚至隐式转换。
Declare @t time = '12:00'
Select UsingConvert = convert(varchar(5),@t,108)
,UsingImplicit = left(@t,5)
结果
UsingConvert UsingImplicit
12:00 12:00
您需要将冒号:
转义为:
。例如:
SELECT FORMAT(startTime, 'hh:mm') as startTime FROM t
结果:
startTime
---------
12:00
13:00
参见示例:db<>fiddle。