如何使用 SQL Server 从当前日期减去 30 天 [英] How to subtract 30 days from the current date using SQL Server

查看:176
本文介绍了如何使用 SQL Server 从当前日期减去 30 天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从当前日期减去 30 天,而且我是 SQL Server 的新手.

I am unable subtract 30 days from the current date and I am a newbie to SQL Server.

这是我专栏里的数据

date 
------------------------------
Fri, 14 Nov 2014 23:03:35 GMT
Mon, 03 Nov 2014 15:18:00 GMT
Tue, 11 Nov 2014 01:24:47 GMT
Thu, 06 Nov 2014 19:13:47 GMT
Tue, 04 Nov 2014 12:37:06 GMT
Fri, 1 Nov 2014 00:33:00 GMT
Sat, 5 Nov 2014 01:06:00 GMT
Sun, 16 Nov 2014 06:37:12 GMT

为了创建上面的列,我使用了 varchar(50) 现在我的问题是我想从日期列显示过去 15-20 天的日期有人可以帮助解决这个问题吗?更新 [如何按顺序显示过去 7 天的日期

For creating the above column I used varchar(50) and now my problem is I want to display the dates for past 15-20 days from the date column can any one help with this issue ? update [ how can i display last 7 days dates in order

推荐答案

可以转换成datetime,然后使用DATEADD(DAY, -30, date).

You can convert it to datetime, and then use DATEADD(DAY, -30, date).

请参阅此处.

编辑

我怀疑很多人发现这个问题是因为他们想从当前日期中减去(正如问题的标题,但不是 OP 的意图).下面 munyul 的评论更具体地回答了这个问题.由于评论被认为是空灵的(可能会在任何给定点被删除),我将在这里重复:

I suspect many people are finding this question because they want to substract from current date (as is the title of the question, but not what OP intended). The comment of munyul below answers that question more specifically. Since comments are considered ethereal (may be deleted at any given point), I'll repeat it here:

DATEADD(DAY, -30, GETDATE())

这篇关于如何使用 SQL Server 从当前日期减去 30 天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆