将给定的分钟数转换为 hh:mm:ss 格式
Converting the given minutes to hh:mm:ss format
这是我的数据库结构(简化):
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
?
您可以转换为时间:
select convert(time, dateadd(minute, 142, 0))
或者,如果您希望将值作为字符串,您可以使用老式的 convert()
:
select convert(varchar(8), dateadd(minute, 142, 0), 108)
但我很困惑。您将该值存储为 time
,其中包含 7 位小数秒。然后你抱怨看到小数秒。
小数秒可能不会影响电影时间,所以为什么不直接使用:
Duration time(0) not null,
这是我的数据库结构(简化):
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
?
您可以转换为时间:
select convert(time, dateadd(minute, 142, 0))
或者,如果您希望将值作为字符串,您可以使用老式的 convert()
:
select convert(varchar(8), dateadd(minute, 142, 0), 108)
但我很困惑。您将该值存储为 time
,其中包含 7 位小数秒。然后你抱怨看到小数秒。
小数秒可能不会影响电影时间,所以为什么不直接使用:
Duration time(0) not null,