Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateTime calculation

Status
Not open for further replies.

Jcarr38

Programmer
Feb 16, 2007
27
US
I'm displaying records and using a field called ratedOn which is a datetime attribute with a getdate() default value. To display records on a page, say i wanted to only show records that are about 7 days old and anything older than 7 days, it won't display. How could i use a calculation in my query...

So for this example below only show records from May31st till today:

ratedon: 2007-06-06 12:00:10.020

I'd appreciate any help.
 
Have a look in SQL Server Help (aka Books On Line) at the dateadd function.

To get rid of time component you can do this:

Code:
select dateadd(day, datediff(day, 0, getdate()), 0)

This uses datediff to get the number of whole days between now and date 0 (1/1/1900). Then adds this to zero to get the date (without hours/min/seconds added).

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
or equal to ?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Check out the FAQ section...there is a section on DATETIME Tips & Tricks. Pretty good reading in there.

-SQLBill

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top