我需要幫助將integer
轉換爲varchar
。SQL將int轉換爲varchar
我試圖寫一個程序,需要在ProfileID
和Currenttime
;使用這兩個值可以找到profileID
的開始時間,並從 starttime
中減去currenttime
,並返回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
有什麼錯誤信息 –
什麼是@hh的價值在錯誤發生的地方?另外,你的邏輯ElapsedTime表明你不是指間隔 –
@hh = 1.C的值從字符串轉換日期和/或時間時,反轉失敗。 – Pat