SQL query that displays “time ago dates” like “one week ago”, “two weeks ago”, “one month ago”, “one year ago”, etc

非 Y 不嫁゛ 提交于 2019-12-06 06:45:34

问题


I need a query that displays dates in the following format:

Dates that fall in the past 7 days -> “one week ago” Dates that fall in the past 7 to 14 days -> “two week ago” Etc…

Dates that fall in the past 30 days -> “one month ago” Dates that follow in the past 30 to 60 days -> “two months ago Etc..

Dates that fall in the past 365 days -> “one year ago” Dates that fall in the past 365 to 730 days -> “two years ago Etc...

If you guys can point me to the right direction I’ll appreciate it.

Thank you


回答1:


As stated above, use a case statement in your SQL query. Something like this:

SELECT 
Column1, 
Column2, 
theDate,
CASE
  WHEN DATEDIFF(dd, theDate, GetDate()) =< 7 THEN 'One Week Ago'
  WHEN DATEDIFF(dd, theDate, GetDate()) > 7 AND DATEDIFF(dd, theDate, GetDate()) < 30 THEN 'One Month Ago'
  -- ...
  END
AS TimeAgo,
Column3,
Column4
FROM Table1

More Information for MS SQL: http://msdn.microsoft.com/en-us/library/ms181765.aspx (Or see the documentation for your SQL server brand)




回答2:


Here is a mysql function I wrote called time_ago

DELIMITER $$
DROP FUNCTION IF EXISTS time_ago;
CREATE FUNCTION time_ago (ts datetime) 
RETURNS varchar(255)
DETERMINISTIC
BEGIN 
    DECLARE utx INT SIGNED DEFAULT 1;
    DECLARE nowutx INT SIGNED DEFAULT 1;
    DECLARE dif INT SIGNED DEFAULT 1;
    DECLARE method varchar(255);
    DECLARE cnt varchar(255);
    DECLARE plural tinyint(11);
    DECLARE future tinyint(11);
    SET utx := UNIX_TIMESTAMP(ts);
    SET nowutx := UNIX_TIMESTAMP(NOW());
    SET future := utx > nowutx;
    SET dif := IF(future, utx - nowutx, nowutx - utx);
        SET method := IF(dif < 60, 'Second', IF(
                                    dif < (60 * 60), 'Minute', IF(
                                        dif < (60 * 60 * 24), 'Hour', IF(
                                            dif < (60 * 60 * 24 * 7), 'Day' , IF(
                                                dif < (60 * 60 * 24 * 365), 'Week', 'Year')))));

        SET cnt := IF(dif < 60, dif, IF(
                                    dif < (60 * 60), floor(dif / 60), IF(
                                        dif < (60 * 60 * 24), floor(dif / (60 * 60)), IF(
                                            dif < (60 * 60 * 24 * 7), floor(dif / (60 * 60 * 24)) , IF(
                                                dif < (60 * 60 * 24 * 365) , floor(dif / (60 * 60 * 24 * 7)), floor(dif / (60 * 60 * 24 * 365)))))));

        SET plural := cnt != 1;

        return CONCAT(IF(future, 'In ', ''), cnt, ' ',method, IF(plural, 's', '') , IF(future, ' From Now', ' Ago'));
END$$
DELIMITER ;

It is used like this

SELECT time_ago(date_ordered) time_ago FROM orders LIMIT 1

And the result looks like this:

time_ago
22 Weeks Ago

EDIT:

I have modified this answer to offer an improved version of this function: the new version uses DECLARE instead of setting session variables.



来源:https://stackoverflow.com/questions/7323883/sql-query-that-displays-time-ago-dates-like-one-week-ago-two-weeks-ago

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!