将int转换为varchar的SQL
我需要帮助将将int转换为varchar的SQL,sql,sql-server,type-conversion,Sql,Sql Server,Type Conversion,我需要帮助将integer转换为varchar 我正在尝试编写一个过程,该过程接受ProfileID和Currenttime;使用这两个值,它会找到profileID的开始时间,并从中减去currenttime starttime并返回hours:minutes:seconds 我做错了什么,有没有更好的方法来写这个 谢谢 CREATE PROCEDURE [dbo].[CalculateElaspedTime] -- Add the parameters for the stored
integer
转换为varchar
我正在尝试编写一个过程,该过程接受ProfileID
和Currenttime
;使用这两个值,它会找到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
试试这个。功能中的所有兴奋可能都是不必要的
CONVERT(varchar(10),(@CurrentDateTime-@Start_Time),108)
您遇到的一个问题是这种说法:
set @StartTime = (Select top 1 [DateTime] From Log WHERE ProfileID = @ProfileID);
它的结果是不确定的,因为SQL不保证结果顺序,除非您在ORDERBY子句中显式指定它。您应该使用ORDER BY或使用像MAX()这样的聚合函数来获取所需的行
你做的工作比必要的多得多。SQL Server(无论如何,最新版本)支持日期算术,减去两个日期的结果就是另一个日期(从SQL Server纪元1900年1月1日00:00:00.000的偏移量)。除非经过的时间超过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
错误消息是什么?错误发生时@hh的值是多少?另一方面,您的ElapsedTime逻辑表明您没有引用间隔。@hh=1的值。从字符串转换日期和/或时间时转换失败。无法计算强制转换(@hh As varchar)是否尝试使用nvarchar(10)替换未指定长度的varchar?在转换为
varchar
时,应始终为其指定一个长度。如果省略该长度,您知道默认值是什么吗?