Date Question

  • In the following code I was expecting the Minutes from the base date but it does from midnight - is it ignoring the date component?


    DECLARE @t TIME = GETDATE();
    SELECT DATEDIFF(MINUTE, 0, @t)

    Thanks,
    Jason

  • What do you mean by "base date"? Also, I'm a bit confused by the your question and the defined: DATEDIFF(MINUTE, 0, @t), which does get the minutes from 0 (meaning midnight) to the current time. As far as I can tell, it works...I got 528 a few minutes ago when I ran your query, which is correct for the number of minutes from midnight until I ran the query. For example, it's 8:50 AM right now, so that would be 8 * 60 + 50, which is 530.
    ???? More details are needed if this doesn't "answer" your question.

    Alan H
    MCSE - Data Management and Analytics
    Senior SQL Server DBA

    Best way to ask a question: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • You are taking Time component of getdate() in @t, so obvious it will ignore the date part. with this script you'll get the minute difference from midnight only. Elaborate the question

  • Jason Toews - Wednesday, August 30, 2017 8:35 AM

    In the following code I was expecting the Minutes from the base date but it does from midnight - is it ignoring the date component?


    DECLARE @t DATETIME = GETDATE();
    SELECT DATEDIFF(MINUTE, 0, @t)

    Thanks,
    Jason

    There FTFY.
    You need to give a date if you want minutes from base date instead of minutes from midnight.
    Maybe this would make it clear.

    DECLARE @t TIME = GETDATE();

    SELECT CAST( @t AS datetime);

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • If by "Base Date", you mean 0... 0 is shorthand for 19-01-01 00:00:00.000... aka midnight of 1/1/1900.
    So DECLARE @t TIME = GETDATE(); SELECT DATEDIFF(MINUTE, 0, @t); is going to calculate the number of minutes between 19-01-01 00:00:00.000 and 19-01-01 12:32:15.083.

  • Sorry my question could be better - I was thinking that I should be getting the minutes from 1/1/1900 not just midnight. For some reason my brain totally glossed over.

    Thanks,
    Jason

  • Jason Toews - Wednesday, August 30, 2017 12:12 PM

    Sorry my question could be better - I was thinking that I should be getting the minutes from 1/1/1900 not just midnight. For some reason my brain totally glossed over.

    Thanks,
    Jason

    No worries. Happens to the best of us. 😉

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply