1

这是我的数据库结构(简化):

if db_id('MovieDB') is null
    create database MovieDB;

go

use MovieDB;

go

if object_id(N'Movies', N'U') is null
create table Movies
(
    MovieID         bigint          not null    identity    primary key,
    Duration        time(7)         not null,
);

insert into Movies values
(format(dateadd(minute, 142, 0), N'hh:mm:ss', 'en-US'));

我将持续时间插入为分钟,我需要将其转换为hh:mm:ss格式。但是,通过执行此操作,我得到02:22:00.0000000.

如何格式化日期以获得公正02:22:00

4

1 回答 1

1

您可以转换为时间:

select convert(time, dateadd(minute, 142, 0))

或者,如果您希望将值作为字符串,您可以使用老式的convert()

select convert(varchar(8), dateadd(minute, 142, 0), 108)

但我很困惑。您将值存储为time7 位数的小数秒。然后你抱怨看到小数秒。

小数秒可能对电影时间没有影响,所以为什么不直接使用:

Duration        time(0)         not null,
于 2019-03-16T14:32:43.227 回答