• jkramprakash (11/24/2015)


    I checked this query with my data.it is returning the following output.

    SELECT last_sign_in_at,

    CAST(DATEDIFF(yy, last_sign_in_at, GETDATE()) AS NVARCHAR(4)) + ' years, ' +

    CAST(DATEDIFF(m, last_sign_in_at, GETDATE()) AS NVARCHAR(2)) + ' months, ' +

    CAST(DATEDIFF(second, last_sign_in_at, GETDATE()) / 60 / 60 / 24 % 7 AS NVARCHAR(2)) + ' days, ' +

    CAST(DATEDIFF(second, last_sign_in_at, GETDATE()) / 60 / 60 % 24 AS NVARCHAR(2)) + ' hours, ' +

    CAST(DATEDIFF(second, last_sign_in_at, GETDATE()) / 60 % 60 AS NVARCHAR(2)) + ' minutes, ' +

    CAST(DATEDIFF(second, last_sign_in_at, GETDATE()) % 60 AS NVARCHAR(2)) + ' seconds ago.' AS date_period

    FROM users

    Output

    ----------

    last_sign_in_at date_period

    2014-09-02 16:22:55 1 years, 14 months, 0 days, 7 hours, 30 minutes, 48 seconds ago.

    Actually i want the output is 1 year, 2 months, 23 day ago....

    suppose the user last_login_date is 2015-11-23 means.it returns 2 days ago.

    Thank you.

    How do you define "One month ago from today"? Is it 28 days ago, or 25th of October?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden