3

我需要帮助将 an 转换integervarchar.

我正在尝试编写一个包含 aProfileID和 a的程序Currenttime;使用这两个值,它会找到 的开始时间并从中 profileID减去并返回。currenttimestarttimehours:minutes:seconds

我做错了什么,有没有更好的方法来写这个?

谢谢。

CREATE PROCEDURE [dbo].[CalculateElaspedTime]
    -- Add the parameters for the stored procedure here
    @ProfileID nvarchar(10),
    @CurrentDateTime datetime = '' 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    if @CurrentDateTime = CAST('' as datetime)
    set @CurrentDateTime = GETDATE()

    DECLARE @StartTime datetime;
    DECLARE @ElaspedTime time;
    DECLARE @hh int;
    DECLARE @mm int;
    DECLARE @ss int;
    Declare @TimeString varchar
    set @StartTime = (Select top 1 [DateTime] From Log WHERE ProfileID = @ProfileID);
    set @hh = DateDiff(hour,@StartTime,@CurrentDateTime);
    set @mm = DateDiff(minute,@StartTime,@CurrentDateTime)-60*@hh;
    set @ss = DateDiff(second,@StartTime,@CurrentDateTime)-60*@mm;

    set @TimeString = (Select CAST(@hh as varchar)); -- Fails Here 
    set @ElaspedTime = convert(datetime, cast(@hh as varchar) + ':' + cast(@mm as varchar) + ':' + cast(@ss as varchar));
    INSERT INTO Log (ElaspedTime) Values (@ElaspedTime);
END
4

2 回答 2

8

尝试这个。函数中的所有这些兴奋可能都是不必要的。

CONVERT(varchar(10),(@CurrentDateTime-@Start_Time),108)
于 2013-07-22T16:45:55.673 回答
1

您遇到的一个问题是以下声明:

set @StartTime = (Select top 1 [DateTime] From Log WHERE ProfileID = @ProfileID);

它的结果是不确定的,因为 SQL 不保证结果顺序,除非您在 ORDER BY 子句中明确指定它。您应该使用 ORDER BY 或使用 MAX() 之类的聚合函数来获取所需的行。

而且你做的工作比必要的多得多。SQL Server(无论如何是最近的版本)支持日期算术,减去两个日期的结果是另一个日期(从 1900 年 1 月 1 日 00:00:00.000 的 SQL Server 纪元偏移。这个更简单的形式应该可以,除非经过时间将超过 1 天:

create procedure dbo.CalculateElaspedTime

  @ProfileID       nvarchar(10)  ,
  @CurrentDateTime datetime = '' 

as

  set nocount on

  declare
    @now     dateTime    ,
    @start   datetime    ,
    @elapsed varchar(32)

  select @now = case coalesce(@currentDateTime,'') when '' then current_timestamp else @currentDateTime end ,
         @start = max( [DateTime] )
  from dbo.Log
  where ProfileId = @profileId

  set @elapsed = convert(varchar,@now-@start,108)

  insert dbo.Log ( ElapsedTime ) Values (@elapsed);

  return 0
go

如果您经过的时间可能超过一天,那么您的原始方法就是您想要的:

create procedure dbo.CalculateElaspedTime

  @ProfileID       nvarchar(10)  ,
  @CurrentDateTime datetime = '' 

as

  set nocount on

  declare @now     dateTime = case coalesce(@currentDateTime,'') when '' then current_timestamp else @currentDateTime end ,
  declare @start   datetime = ( select max([DateTime]) from dbo.Log where profileId = @profileId )
  declare @elapsed int      = select datediff(ss,@now,@start)
  declare
    @hh int ,
    @mm int ,
    @ss int

  set @hh      = @elapsed / 3600 -- 3600 is seconds/hour
  set @elapsed = @elapsed % 3600 
  set @mm      = @elapsed / 60   -- 60 is seconds/minute
  set @elapsed = @elapsed % 60
  set @ss      = @elapsed / 1   -- 1 is seconds/second :)

  declare @hhmmss =       right('00'+convert(varchar,@hh),2)
                  + ':' + right('00'+convert(varchar,@mm),2)
                  + ':' + right('00'+convert(varchar,@ss),2)

  insert dbo.Log ( ElapsedTime ) Values (@hhmmss);

  return 0
go
于 2013-07-22T17:19:50.483 回答