Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all
i have this table structure :
SQL
title varchar(50)
_Current_date varchar(50)


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

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

insert into table1 values(title, _date);


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.
Posted

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:

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

SELECT
  *
FROM
  dbo.MyTable
WHERE
  MyDateField > @EarliestDate
 
Share this answer
 
v2
Hi,

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

Regards,
GVPrabu
 
Share this answer
 
v2
Comments
ocean99 17-Jun-13 10:22am    
I try hhis, don't return any thing !
gvprabu 17-Jun-13 11:03am    
I updated, Check now
i use this :

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


it run , thank every one ^_^
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900