Rounding Dates in MySQL 5

by timvasil 2/20/2008 12:19:00 AM

A while back I blogged about how to round dates in SQL Server 2000+.  Here's the MySQL port:

DROP FUNCTION IF EXISTS `dbname`.`fn_RoundDate` $$
CREATE FUNCTION `dbname`.`fn_RoundDate` (dateArg DATETIME, formatArg CHAR) RETURNS DATETIME
DETERMINISTIC
BEGIN

 IF dateArg IS NULL OR formatArg IS NULL THEN
    RETURN NULL;
  ELSEIF formatArg = 'T' THEN -- Time
    RETURN CAST(dateArg AS TIME);
  ELSE
    -- Round to day
    SET dateArg = CAST(dateArg AS DATE);

   RETURN CASE formatArg
    WHEN 'D' THEN -- Day
     CAST(dateArg AS DATE)
    WHEN 'W' THEN -- Week
        ADDDATE(dateArg, INTERVAL 1 - DAYOFWEEK(dateArg) DAY)
    WHEN 'M' THEN -- Month
        ADDDATE(dateArg, INTERVAL 1 - DAYOFMONTH(dateArg) DAY)
      WHEN 'Q' THEN -- Quarter
        ADDDATE(MAKEDATE(YEAR(dateArg), 1), INTERVAL FLOOR(MONTH(dateArg) / 4) QUARTER)
    WHEN 'Y' THEN -- Month
        ADDDATE(dateArg, INTERVAL 1 - DAYOFYEAR(dateArg) DAY)
    ELSE -- Unknown format
     dateArg
    END;
  END IF;

END $$

DELIMITER ;

Why there isn't a built-in function to do something so useful--in either SQL Server or MySQL--is beyond me.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

MySQL

Rounding Dates in SQL Server 2000 and 2005

by timvasil 11/5/2007 2:38:00 AM

SQL Server's native support for date formatting and rounding is pathetic.  SQL Server stores dates as 2 4-byte integers:  one for the date and one for the time, and yet there's no way to extract the date or time portion--despite the datatype being set up in such a way to explicitly optimize for this operation!

Sick of reinventing the wheel, I finally broke down and wrote my own function to round dates to a time, day, week, month, quarter, or year:

CREATE FUNCTION fn_RoundDate(@Date DATETIME, @Format CHAR) RETURNS DATETIME
AS
BEGIN
       IF @Date IS NULL RETURN NULL
       RETURN CASE @Format
            WHEN 'T' THEN -- Time
                  @Date - FLOOR(CAST(@Date AS FLOAT))
            WHEN 'D' THEN -- Day
                  FLOOR(CAST(@Date AS FLOAT))
            WHEN 'W' THEN -- Week
                  DATEADD(ww, DATEPART(ww, @Date) - 1, CONVERT(DATETIME, '1/1/' + CAST(YEAR(@Date) AS VARCHAR(4)), 101))
            WHEN 'M' THEN -- Month
                  CONVERT(DATETIME, CAST(MONTH(@Date) AS VARCHAR(2)) + '/1/' + CAST(YEAR(@Date) AS VARCHAR(4)), 101)
            WHEN 'Q' THEN -- Quarter
                  CONVERT(DATETIME, CAST(((MONTH(@Date) - 1) / 3) * 3 + 1 AS VARCHAR(2)) + '/1/' + CAST(YEAR(@Date) AS VARCHAR(4)), 101)
            WHEN 'Y' THEN -- Year
                  CONVERT(DATETIME, '1/1/' + CAST(YEAR(@Date) AS VARCHAR(4)), 101)
            ELSE -- Unknown format
                  @Date
      END
END 

Here is sample usage: 

 

DECLARE @Date DATETIME;
SET @Date = '11/5/07 2:42 AM';
SELECT dbo.fn_RoundDate(@Date, 'T') AS Time,
       dbo.fn_RoundDate(@Date, 'D') AS Day,
       dbo.fn_RoundDate(@Date, 'W') AS Week,
       dbo.fn_RoundDate(@Date, 'M') AS Month,
       dbo.fn_RoundDate(@Date, 'Q') AS Quarter,
       dbo.fn_RoundDate(@Date, 'Y') AS Year

With output as follows:

  • Time = 1900-01-01 02:42:00.000
  • Day = 2007-11-05 00:00:00.000
  • Week = 2007-11-05 00:00:00.000
  • Month = 2007-11-01 00:00:00.000
  • Quarter = 2007-10-01 00:00:00.000
  • Year = 2007-01-01 00:00:00.000

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SQL Server

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments