SQL Server将日期时间转换为n周期
我有一个字段 lastLogin ,它是datetime。
如果用户的最后一次登录时间是年龄的
,那么如果不到1年,而不是
n个月前,如果比前一天的
少1个月,如果不到那个1天比
n小时前,如果距离分钟前的
少于1小时,如果距离不到1分钟
n秒前
I have a field lastLogin which is datetime.
I want to show the user his/her last login time as
n years ago, if it is less that 1 year than
n months ago, if it is less that 1 month than
n days ago, if it is less that 1 day than
n hours ago, if it is less that 1 hour than
n minutes ago, if it is less that 1 minutes than
n seconds ago
我希望 在几年,几个月,几天,微软和秒会自动作为英文语法规则处理。
I want that the s in years , months , days , hours , minures and seconds are handled automatically as the rules of English Grammar.
我是sql中的一个完整的初学者,我正在使用 SQL SERVER 2008 R2
i am a complete beginner in sql and i am using SQL SERVER 2008 R2
您可以使用 CASE
语句
CASE
WHEN DATEDIFF(yy, lastLogin, GETDATE()) > 1 THEN CASE(DATEDIFF(yy, lastLogin, GETDATE()) AS varchar(5) + ' years ago'
WHEN DATEDIFF(y, lastLogin, GETDATE()) = 1 THEN '1 year ago'
-- ... and so on
END AS LastLoginPeriod
未测试和写入晚饭准备好了!
Not tested and written in a hurry as supper is ready!