SQL将int转换为varchar

时间:2013-07-22 16:21:01

标签: sql sql-server type-conversion

我需要帮助将integer转换为varchar

我正在尝试编写一个包含ProfileIDCurrenttime的过程;使用这两个值,它会找到profileID的开始时间并从中减去currenttime starttime并返回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

2 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分: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