获得全部时间

发布于 2024-08-11 01:25:45 字数 227 浏览 5 评论 0原文

我怎样才能获得像这样的整个时间 datediff(time, logindate, logoutdate)

我知道这个内置函数不接受时间参数,但我怎样才能获得整个时间而不是分钟,毫秒、第二等?

logindate datetime2
logoutdate datetime2

我想要像 1:05:45 这样的东西而不是它的一部分。

How can I get the whole time like this datediff(time, logindate, logoutdate)

I know this built-in function doesn't accept time argument but how can I get the whole time rather than minute, millisecond, second etc. ?

logindate datetime2
logoutdate datetime2

I want something like 1:05:45 rather than portion of it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帅的被狗咬 2024-08-18 01:25:45

试试这个

create table dbo.UserLog (UserID VARCHAR(32),loginDate DATETIME,logoutDate DATETIME)

insert into userLog VALUES ('SPARKY','11/14/2009 3:25pm',getDate())
insert into userLog VALUES ('JANNA','11/14/2009 10:45am',getDate())

select UserId,loginDate,logoutDate,
    convert(varchar(12),dateAdd(mi,datediff(mi,logindate,logoutdate),'Jan  1 1753 12:00AM'),114) as timeSpent
FROM userLog

基本上,将日期之间的分钟差添加到最早的有效 SQL 日期,并返回格式化为时间的值。

Try this

create table dbo.UserLog (UserID VARCHAR(32),loginDate DATETIME,logoutDate DATETIME)

insert into userLog VALUES ('SPARKY','11/14/2009 3:25pm',getDate())
insert into userLog VALUES ('JANNA','11/14/2009 10:45am',getDate())

select UserId,loginDate,logoutDate,
    convert(varchar(12),dateAdd(mi,datediff(mi,logindate,logoutdate),'Jan  1 1753 12:00AM'),114) as timeSpent
FROM userLog

Basically, adding the minutes difference between the dates to the earliest valid SQL date and returning the value formatted as a time.

○愚か者の日 2024-08-18 01:25:45

以天数为单位的差异:

select cast(logoutdate - logindate as float) from table_name

或者只是

select logoutdate - logindatefrom table_name

您可以从中评估天数、小时数、分钟数。

编辑

将其格式化为时间:

SELECT CONVERT(VARCHAR,DATA_KOSZTU - DATA_OST_ZMIANY,108) FROM TR_KOSZT

如果用户超过 24 小时没有登录,它将起作用,因为 CONVERT 用于格式化日期时间,而不是时间跨度。

To have difference in days:

select cast(logoutdate - logindate as float) from table_name

or just

select logoutdate - logindatefrom table_name

You can evaluate days, hours, minutes from it.

EDIT

To have it formatted as time:

SELECT CONVERT(VARCHAR,DATA_KOSZTU - DATA_OST_ZMIANY,108) FROM TR_KOSZT

It will work if users are not logged for more than 24 hours, because CONVERT is used to format datetime, not timespan.

眼波传意 2024-08-18 01:25:45

由于 MSSQL 没有 timepsan 数据类型,因此以毫秒到年的绝对整数返回的 datediff 应该足以让您在 .NET 中创建 TimeSpan 的实例。

Because MSSQL do not have timepsan datatype, datediff returning in absolute integer from milliseconds to years should be enough for you to create a instance of say TimeSpan in .NET.

我是有多爱你 2024-08-18 01:25:45

您所说的 Sql Server 版本是什么?至少在 SQL Server 2000 及更高版本中,

SELECT datediff(ss,'2006-11-10 05:47:53.497','2006-11-10 05:48:10.420')

将为您提供这两个日期时间之间的差异(以秒为单位)。

What Sql Server version are you talking about? In SQL Server 2000 and later, at least,

SELECT datediff(ss,'2006-11-10 05:47:53.497','2006-11-10 05:48:10.420')

will give you the difference between those two datetimes in seconds.

一萌ing 2024-08-18 01:25:45

例如
选择 CONVERT(varchar(10),GETDATE(),108)

E.g.
select CONVERT(varchar(10),GETDATE(),108)

我一直都在从未离去 2024-08-18 01:25:45

这是您正在寻找的解决方案。


DECLARE @Date1 datetime
DECLARE @Date2 datetime 
SET @Date2 = '2006-11-15 07:26:25.000'
SET @Date1 = '2009-11-15 05:35:45.000'
--            -----------------------
-- Math done by hand      1:50:40
--
DECLARE @TotalSeconds bigint
DECLARE @Hours        bigint
DECLARE @Minutes      bigint
DECLARE @Seconds      bigint
DECLARE @HH           varchar(20)
DECLARE @MM           varchar(2)
DECLARE @SS           varchar(2)
DECLARE @Result       varchar(50)
--
SET @TotalSeconds = datediff(ss,@Date1 ,@Date2)
SET @Hours        = FLOOR(@TotalSeconds / 3600)
SET @TotalSeconds = @TotalSeconds % 3600
SET @Minutes      = FLOOR(@TotalSeconds / 60)
SET @Seconds      = @TotalSeconds % 60  
--
SET @HH = CAST(@Hours   as varchar)
SET @MM = CAST(@Minutes as varchar)
SET @SS = CAST(@Seconds as varchar)
IF @Minutes < 10 SET @MM = '0' + @MM
IF @Seconds < 10 SET @SS = '0' + @SS
--
SET @Result = @HH + ':' + @MM + ':' + @SS
SELECT @Result


Here's the solution you are looking for.


DECLARE @Date1 datetime
DECLARE @Date2 datetime 
SET @Date2 = '2006-11-15 07:26:25.000'
SET @Date1 = '2009-11-15 05:35:45.000'
--            -----------------------
-- Math done by hand      1:50:40
--
DECLARE @TotalSeconds bigint
DECLARE @Hours        bigint
DECLARE @Minutes      bigint
DECLARE @Seconds      bigint
DECLARE @HH           varchar(20)
DECLARE @MM           varchar(2)
DECLARE @SS           varchar(2)
DECLARE @Result       varchar(50)
--
SET @TotalSeconds = datediff(ss,@Date1 ,@Date2)
SET @Hours        = FLOOR(@TotalSeconds / 3600)
SET @TotalSeconds = @TotalSeconds % 3600
SET @Minutes      = FLOOR(@TotalSeconds / 60)
SET @Seconds      = @TotalSeconds % 60  
--
SET @HH = CAST(@Hours   as varchar)
SET @MM = CAST(@Minutes as varchar)
SET @SS = CAST(@Seconds as varchar)
IF @Minutes < 10 SET @MM = '0' + @MM
IF @Seconds < 10 SET @SS = '0' + @SS
--
SET @Result = @HH + ':' + @MM + ':' + @SS
SELECT @Result


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文