我怎样才能获得过去30天的所有行? [英] How can i get all rows for last 30 days ?

查看:126
本文介绍了我怎样才能获得过去30天的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧

i有这个表结构:

Hi all
i have this table structure :

title varchar(50)
_Current_date varchar(50)




$ b $通过aspx页填充表,我使用此代码(伪代码):





i fill table via aspx page, that i use this code (pseudo code) :

string tilte ="MyTitlr";
string _date = DateTime.Now.ToString("d");

insert into table1 values(title, _date);





现在:



我如何获得过去30天的所有头衔?

(我如何选择所有记录在过去的30天里插入了吗?)



祝你好运。



Now :

How can i get all titles for last 30 days ?
( How can i select all records that have been inserted in last 30 days ? )

Best regards.

推荐答案

在SQL中你使用的是 GETDATE()获取当前日期和时间的功能,或者如果您只想要日期,则需要删除可以这样做的时间部分:



DATEADD(dd,0,DATEDIFF(dd,0,GETDATE()))



DATEADD 函数可用于将该日期缩短30天:



DATEADD(天,-30,GETDATE())



然后使用 WHERE 语句中的子句用于过滤结果。



结果是这样的:



In SQL you use the GETDATE() function to get the current date and time or if you want just the date, you need to remove the time portion which can be done like this:

DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

The DATEADD function can be used to reduce that date by 30 days:

DATEADD(days, -30, GETDATE())

You then use the WHERE clause of your statement to filter the result.

The result being something like this:

DECLARE @EarliestDate AS DATETIME
SET @EarliestDate = DATEADD(days, -3, DATEDIFF(dd, 0, GETDATE()))

SELECT
  *
FROM
  dbo.MyTable
WHERE
  MyDateField > @EarliestDate






你可以像这样使用查询...

Hi,

can u use Query like this...
SELECT Column_List FROM table_Name WHERE InsertDate BETWEEN convert(char(10),dateadd(dd,-30,GETDATE()),112) AND convert(char(10),GETDATE(),112)



问候,

GVPrabu


Regards,
GVPrabu


我用这个:



i use this :

SELECT  title,CONVERT(datetime,_date,103) FROM Table1 where CONVERT(datetime,_date,103) > dateadd(dd,-30,GETDATE())





它运行,谢谢每一个^ _ ^



it run , thank every one ^_^


这篇关于我怎样才能获得过去30天的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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