Solved

select only rows in database based on today's date

Posted on 2013-05-27
3
765 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

739 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