How to calculate days between two dates,

Lasse

Is it possible in MySQL to easily to get the number of days between two dates, group by months and then disperse those days between the months, so that when I group by month, I get to see how many days inbetween two those two dates fall into each month.

Example: creation 05.05.2014 to expiration 06.06.2014 returns 32 days. I want to display this as:

May: 26 days June: 5 days

So far I've got:

SELECT MONTHNAME(date) as month_name,
SUM(DATEDIFF(expiration, date)) AS num_days
FROM reservations
GROUP BY month_name

But it doesn't disperse the days corretly by month, just drop them all into the group-by -start-date-month, obviously. Any ideas would be highly appriciated.

Jermaine

I would build a comprehensive date temp table (or CTE) first.

From there you can select what you'd like, and group it however suits your needs.

    IF OBJECT_ID('TEMPDB..#Dates') IS NOT NULL DROP TABLE #Dates

    DECLARE @StartDate DATETIME, @EndDate DATETIME

        SET @StartDate = '05/05/2014'
        SET @EndDate = '06/06/2014'

    ;WITH Dates AS
        (
         SELECT CAST(CONVERT(VARCHAR(8), @StartDate,112) AS INT) AS DateId
              , CAST(@StartDate AS DATE) AS Date
              , CAST(@StartDate AS DATETIME) AS DateTime
              , DATEPART(QUARTER, @StartDate) AS QuarterId
              , 'Q' + CAST(DATEPART(QUARTER,@StartDate) AS CHAR(1)) AS Quarter
              , DATEPART(MONTH, @StartDate) AS MonthId
              , DATENAME(MONTH, @StartDate) AS Month
              , DATEPART(DAY, @StartDate) AS Day
              , DATEPART(YEAR, @StartDate) AS Year
              , DATENAME(DW,@StartDate) AS DayOfWeek
              , DATEPART(DAYOFYEAR, @StartDate) AS DayOfYear

          UNION ALL

         SELECT CAST(CONVERT(VARCHAR(8),DATEADD(DAY, 1, D.DateTime), 112) AS INT) ASDateId
              , CAST(DATEADD(DAY, 1, D.DateTime) AS DATE) AS Date
              , CAST(DATEADD(DAY, 1, D.DateTime) AS DATETIME) AS DateTime
              , DATEPART(QUARTER,DATEADD(DAY,1,D.DateTime)) AS QuarterId
              ,'Q'+CAST(DATEPART(QUARTER,DATEADD(DAY, 1, D.DateTime)) AS CHAR(1)) AS Quarter
              , DATEPART(MONTH,DATEADD(DAY, 1, D.DateTime)) AS MonthId
              , DATENAME(MONTH,DATEADD(DAY, 1,D.DateTime)) AS Month
              , DATEPART(DAY,DATEADD(DAY, 1, D.DateTime)) AS Day
              , DATEPART(YEAR,DATEADD(DAY, 1, D.DateTime)) AS Year
              , DATENAME(DW,DATEADD(DAY,1, D.DateTime)) AS DayOfWeek
              , DATEPART(DAYOFYEAR,DATEADD(DAY,1, D.DateTime)) AS DayOfYear
           FROM Dates AS D
          WHERE D.DateTime < @EndDate
        )

    SELECT DateId AS DateId
         , Date AS Date
         , DateTime AS DateTime
         , QuarterId AS QuarterId
         , Quarter AS Quarter
         , MonthId AS MonthId
         , Month AS Month
         , Day AS Day
         , Year AS Year
         , DayOfWeek AS DayOfWeek
         , DayOfYear AS DayOfYear
         , ROW_NUMBER() OVER(ORDER BY DateId)-1 AS DayCount
      INTO #Dates
      FROM Dates AS D
     ORDER BY D.DateId
    OPTION (MAXRECURSION 32767)

    -- SELECT * FROM #Dates

SELECT COUNT(Day) AS CountOfDays, Month
  FROM #Dates
  GROUP BY Month

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate Days Between Two Dates

From Dev

How can I calculate the number of working days between two dates

From Java

How to calculate number of days between two given dates?

From Dev

How to calculate the number of days between two given dates

From Dev

How to calculate the number of days between two given dates in processing 3?

From Dev

How to calculate no of weeks , days past between two dates

From Dev

How to calculate days of weeks between two dates with SQLite in Android

From Dev

How can I calculate the number of working days between two dates

From Dev

how to calculate only days between two dates in postgres sql query .

From Java

Calculate days between two dates in Java 8

From Java

Calculate difference between two dates (number of days)?

From Dev

Calculate number of days between two dates?

From Dev

awk to calculate number of days between two dates:

From Dev

Calculate no of days between two dates in java

From Dev

Calculate number of days between two dates with momentsjs

From Dev

Calculate number of days between two dates in r

From Dev

Calculate days between two dates ignoring year

From Dev

Android calculate days between two dates

From Dev

Calculate no of days between two dates in java

From Dev

Cocoa - Calculate working days between two dates

From Dev

Calculate difference between two calender dates in days

From Dev

How to calculate Number of 'Working days' between two dates in Javascript using moment.js?

From Dev

How to calculate the working(excluding weekends) days in between two different dates in JAVA?

From Dev

Without using packages, how to calculate number of years,months,days between two dates in python

From Dev

And again about calculate the number of days between two dates using JavaScript

From Dev

Calculate total business working days between two dates

From Dev

Calculate the number of business days between two dates in power pivot

From Dev

Calculate working days between two dates in Javascript excepts holidays

From Dev

Calculate number of days between two dates (without using modules)

Related Related

  1. 1

    Calculate Days Between Two Dates

  2. 2

    How can I calculate the number of working days between two dates

  3. 3

    How to calculate number of days between two given dates?

  4. 4

    How to calculate the number of days between two given dates

  5. 5

    How to calculate the number of days between two given dates in processing 3?

  6. 6

    How to calculate no of weeks , days past between two dates

  7. 7

    How to calculate days of weeks between two dates with SQLite in Android

  8. 8

    How can I calculate the number of working days between two dates

  9. 9

    how to calculate only days between two dates in postgres sql query .

  10. 10

    Calculate days between two dates in Java 8

  11. 11

    Calculate difference between two dates (number of days)?

  12. 12

    Calculate number of days between two dates?

  13. 13

    awk to calculate number of days between two dates:

  14. 14

    Calculate no of days between two dates in java

  15. 15

    Calculate number of days between two dates with momentsjs

  16. 16

    Calculate number of days between two dates in r

  17. 17

    Calculate days between two dates ignoring year

  18. 18

    Android calculate days between two dates

  19. 19

    Calculate no of days between two dates in java

  20. 20

    Cocoa - Calculate working days between two dates

  21. 21

    Calculate difference between two calender dates in days

  22. 22

    How to calculate Number of 'Working days' between two dates in Javascript using moment.js?

  23. 23

    How to calculate the working(excluding weekends) days in between two different dates in JAVA?

  24. 24

    Without using packages, how to calculate number of years,months,days between two dates in python

  25. 25

    And again about calculate the number of days between two dates using JavaScript

  26. 26

    Calculate total business working days between two dates

  27. 27

    Calculate the number of business days between two dates in power pivot

  28. 28

    Calculate working days between two dates in Javascript excepts holidays

  29. 29

    Calculate number of days between two dates (without using modules)

HotTag

Archive