• pstanislav (8/26/2016)


    I agree with the comments here - the code as written will produce variable results as the separate GETDATE functions are evaluated. This can be avoided by getting and holding the current time and using that variable instead:

    DECLARE @CurrDtTime AS DATETIME = GETDATE();

    SELECT @CurrDtTime

    UNION

    SELECT CAST(@CurrDtTime AS DATETIME2)

    UNION

    SELECT CAST(@CurrDtTime AS DATETIME)

    UNION

    SELECT CAST(@CurrDtTime AS SMALLDATETIME)

    UNION

    SELECT CAST(@CurrDtTime AS DATETIME2(7))

    +1.

    But even then the answer is not certain.

    If you happen to run the query on an exact minute it will return 1 record.

    Try this:

    DECLARE @CurrDtTime AS DATETIME = DATEADD(n, DATEDIFF(n, 0, GETDATE()), 0);

    _____________
    Code for TallyGenerator