显示“时间前日期”的SQL查询像“一周前”,“两周前”,“一个月前”,“一年前”等

编程入门 行业动态 更新时间:2024-10-23 05:44:15
本文介绍了显示“时间前日期”的SQL查询像“一周前”,“两周前”,“一个月前”,“一年前”等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个以下列格式显示日期的查询:

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

过去7天内的日期 - >一周前日期过去7天到14天 - >两周前等...

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…

过去30天内的日子 - >一个月以前在过去30到60天之间的日期 - >两个月前 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..

过去的日期365天 - >一年前过去365天到730天的日期 - >两年前等...

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.

谢谢

推荐答案

如上所述,在SQL查询中使用case语句。这样的一个例子:

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

有关MS SQL的更多信息: msdn.microsoft/en-us/library/ms181765.aspx (或查看您的SQL服务器品牌的文档)

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

更多推荐

显示“时间前日期”的SQL查询像“一周前”,“两周前”,“一个月前”,“一年前”等

本文发布于:2023-10-28 23:28:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两周   月前   日期   时间   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!