Solved

returning all records entered in a week

Posted on 1997-11-17
2
223 Views
Last Modified: 2012-08-13
I have two problems with SQL

problem 1

I'm using LiveWire Pro to connect to a DB in MS-SQL.  I need to return all records entered in the last week or the last 10 days or 2 weeks etc. etc.

problem 2

I can entered carriage returns in my textbox which are stored in the DB.  Whenever I retrieve the data from the DB, CR's are stripped out.  I'm not putting the data from the DB back into a textbox - just into a formatted table
0
Comment
Question by:bode
2 Comments
 
LVL 10

Accepted Solution

by:
bret earned 100 total points
ID: 1089131
1)  

declare @days_back
select @days_back = -7
Select * from table
where enter_date > dateadd(day,-7,getdate()).

The sql above will get you all records
entered in the last 7 days.  If you run this at 2:22 pm
on Monday, Nov 17th, you will get everything entered since
2:22 pm Monday, Nov 10th.  If you want everything from the
midnight preceeding Nov 10th, you need to strip the time information from the datetime [note that any date will
work as the initial @referencedate, it is completely arbitrary]:

declare @referencedate datetime
select @referencedate = "1/1/1950"
select @referencedate = dateadd(day,datediff(day,@referencedate,getdate()),@referencedate)
select * from mytable
where insert_date > dateadd('day',-7,@referencedate)

2)  Don't know.  (These are two completely unrelated questions.
You really should open them as seperate questions (at half the points each, perhaps))
0
 

Author Comment

by:bode
ID: 1089132
Thanks Bret

I've actually already solved the problem!  I used the following:

SELECT * FROM mytable WHERE DATEDIFF(DD, mydatefield, GETDATE()) BETWEEN 0 AND 7

This selects all records entered in the last 7 days
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now