将两个日期时间戳与第一个日期和第二个日期相结合

时间:2017-12-19 20:57:02

标签: sql sql-server stored-procedures

我有一个表,用于在两个不同的列中存储约会的开始日期和结束日期。它们都是日期时间。它便于输入旧数据。我不能改变结构。我正在使用Microsoft SQL Server。

ApptDate                   ApptTime
2012-02-16 00:00:00.000    1899-12-30 11:45:00.000
2012-02-16 00:00:00.000    1899-12-30 13:15:00.000
2012-02-16 00:00:00.000    1899-12-30 13:30:00.000

我需要做的是将这两个日期时间合二为一。我正在调用存储过程。我的问题是我最好的做法是什么?

ALTER PROCEDURE [dbo].[GetTodaysAppointsNonRepeating] 
    -- Add the parameters for the stored procedure here
    @DateToday DATETIME, @ClientID INT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT a.ID, a.ApptDate, a.ApptTime, a.ApptLength, a.checkedin, a.Entered, a.UserID, a.ProviderID, a.PatientID, CASE WHEN patient.FirstName is null then a.Name END as name, 
    CASE WHEN HomePhone !='' then HomePhone WHEN WorkPhone !='' THEN WorkPhone ELSE CellPhone END as Telephone
    FROM Appointments as a INNER JOIN Patient on a.PatientID = patient.id and 
    a.clientid = patient.clientid WHERE ApptDate = @DateToday and a.ClientID = @ClientID
    AND RepeatType = 'N'
END

1 个答案:

答案 0 :(得分:0)

这就是将两个日期时间字段组合在一起的方式:

SELECT DATETIMEFROMPARTS(YEAR(ApptDate)
                        ,MONTH(ApptDate)
                        ,DAY(ApptDate)
                        ,DATEPART(HOUR,ApptTime)
                        ,DATEPART(MINUTE,ApptTime)
                        ,DATEPART(SECOND,ApptTime)
                        ,DATEPART(MILLISECOND,ApptTime))