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

SQL Server:将Datetime2转换为时间(7),sql,sql-server,datetime,time,converter,Sql,Sql Server,Datetime,Time,Converter,我正在尝试比较datetime2和time(7) 我需要帮助转换datatime2 这就是我尝试过的: DECLARE @PracticeStartTime time(7) DECLARE @PracticeEndTime time(7) SET @PracticeStartTime = ( SELECT PBH.StartTime FROM PracticePractitioner AS PP WITH (NOLOCK) LEFT JOIN PracticeBusin

我正在尝试比较
datetime2
time(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
你的语法不正确

我会使用
CAST

cast(@ScheduledStart as time(7))
您可以使用
CONVERT

convert(time(7), @ScheduledStart)

你有错误吗?这是MSSQL吗?@JChao sql错误:Msg 102,级别15,状态1,过程指定ByModalCreate,第57行“@ScheduledStart”附近语法不正确。