Best way to convert DateTime to “n Hours Ago” in SQL

前端 未结 7 1819
醉酒成梦
醉酒成梦 2021-01-18 03:52

I wrote a SQL function to convert a datetime value in SQL to a friendlier \"n Hours Ago\" or \"n Days Ago\" etc type of message. And I was wondering if there was a better wa

7条回答
  •  情歌与酒
    2021-01-18 04:11

    In Oracle:

    select
      CC.MOD_DATETIME,
      'Last modified ' ||
      case when (sysdate - cc.mod_datetime) < 1
           then round((sysdate - CC.MOD_DATETIME)*24) || ' hours ago'
           when (sysdate - CC.MOD_DATETIME) between 1 and 7
           then round(sysdate-CC.MOD_DATETIME) || ' days ago'
           when (sysdate - CC.MOD_DATETIME) between 8 and 365
           then round((sysdate - CC.MOD_DATETIME) / 7) || ' weeks ago'
           when (sysdate - CC.MOD_DATETIME) > 365   
           then round((sysdate - CC.MOD_DATETIME) / 365) || ' years ago'
           end
    from 
      customer_catalog CC
    

提交回复
热议问题