Solved

Filtering Date (Show All Records)

Posted on 2013-12-24
2
429 Views
Last Modified: 2013-12-24
Hello,

I filter a SQL statement to allow records to be displayed that sit within a specific date range, i.e. show all records from the last 30 days

WHERE
object_audit_info.action_date >= DateAdd(Day, DateDiff(Day, 0, GetDate()) - @NoOfDays, 0)

I do this by passing an int containing the filter value @NoOfDays, the trouble I now have is how to accommodate where I want to display all records?

At the moment when the user requests this the system passes 0, if there any way of telling the SQL statement not run the where clause if the parameter @NoOfDays contains a 0 ?

Thanks.
0
Comment
Question by:andyw27
2 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 39737505
you cna Add OR statement

WHERE
@NoOfDays = 0 OR
object_audit_info.action_date >= DateAdd(Day, DateDiff(Day, 0, GetDate()) - @NoOfDays, 0)
0
 

Author Comment

by:andyw27
ID: 39737519
Brilliant thanks, worked like a charm.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

910 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

19 Experts available now in Live!

Get 1:1 Help Now