簡體   English   中英

SQL將int轉換為varchar

[英]SQL Converting int to varchar

我需要幫助將integer轉換為varchar

我正在嘗試編寫一個包含ProfileIDCurrenttime ; 利用這兩個值它找到的起始時間profileID並減去currenttimestarttime和返回hours: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

嘗試這個。 所有這些興奮的功能都可能是不必要的。

CONVERT(varchar(10),(@CurrentDateTime-@Start_Time),108)

你有一個問題就是這個說法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM