SQL Server:将Datetime2转换为时间(7)

时间:2014-07-21 13:52:45

标签: sql sql-server datetime time converter

我正在尝试将datetime2time(7)

进行比较

我需要帮助转换datatime2

这是我尝试过的:

DECLARE @PracticeStartTime time(7)
DECLARE @PracticeEndTime time(7)

SET @PracticeStartTime = (
    SELECT PBH.StartTime
    FROM PracticePractitioner AS PP WITH (NOLOCK)
    LEFT JOIN PracticeBusinessHours AS PBH WITH (NOLOCK)
        ON PBH.PracticeId = PP.PracticeId
    WHERE PP.PractitionerId = @PractitionerId
    )
SET @PracticeEndTime = (
    SELECT PBH.EndTime
    FROM PracticePractitioner AS PP WITH (NOLOCK)
    LEFT JOIN PracticeBusinessHours AS PBH WITH (NOLOCK)
        ON PBH.PracticeId = PP.PracticeId
    WHERE PP.PractitionerId = @PractitionerId
    )

IF (
    (
        CONVERT(@ScheduledStart AS TIME) BETWEEN @PracticeStartTime
            AND @PracticeEndTime
        )
    AND (
        CONVERT(@ScheduledEnd AS TIME) BETWEEN @PracticeStartTime
            AND @PracticeEndTime
        )
    )
BEGIN
    PRINT 'Time within practice hours';
END
ELSE
BEGIN
    RETURN - 1
END

1 个答案:

答案 0 :(得分:1)

您的语法已关闭。

我会使用CAST

cast(@ScheduledStart as time(7))

您可以使用CONVERT

convert(time(7), @ScheduledStart)