Solved

select only rows in database based on today's date

Posted on 2013-05-27
3
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

615 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