简体   繁体   English

SQL Server:将Datetime2转换为time(7)

[英]SQL Server : convert Datetime2 into time(7)

I am trying to compare a datetime2 with a time(7) 我正在尝试将datetime2time(7)进行比较time(7)

I need help converting the datatime2 . 我需要转换datatime2帮助。

This is what I've tried: 这是我尝试过的:

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

Your syntax is off. 您的语法已关闭。

I would use CAST 我会使用CAST

cast(@ScheduledStart as time(7))

You could use CONVERT 您可以使用CONVERT

convert(time(7), @ScheduledStart)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM