Solved

select only rows in database based on today's date

Posted on 2013-05-27
3
737 Views
Last Modified: 2013-05-27
I am a beginner at SQL. I have MS SQL 2008 database table that has a datetime column. When a new row populates to this table, the server time gets populated to this column.

I need to write three queries:

1. a daily query result of only records with today's date
2. a weekly query with only the last 7 days that runs on Friday (query would be last saturday to this friday)
3. a monthly query run on last day of month.

Any pointers / how to links would be helpful. Thanks in advance.
0
Comment
Question by:Mark B
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 39199851
1. This will give you everthing from the point where the datetime field has the date and all zeroes for the time (i.e. just as today starts).
select *
from <tablename>
where <datefieldname> >= cast(getdate() as date)

Open in new window


2. Same as before, but subtract 6 days (i.e. add negative 6 days).
select *
from tablename
where datefieldname >= dateadd(d -6, cast(getdate() as date))

Open in new window


3. If it's a monthly query you'll run it on the first of the next month, to get everything that happened ... so it'll be up to the start of this month and from the same place last month
select *
from tablename
where datefieldname >= dateadd(m, -1, cast(getdate() as date))
and  datefieldname <= cast(getdate() as date)

Open in new window


hth

Mike
0
 

Author Comment

by:Mark B
ID: 39199895
This worked great, thank you.
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 39200094
Just here to help :)   In fact, I was doing that just a couple of weeks ago, so the code was fresh in my head.  Hence it was quick to type out and post.  Hope you're having a good weekend.

Mike
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

708 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

16 Experts available now in Live!

Get 1:1 Help Now