Solved

Filtering Date (Show All Records)

Posted on 2013-12-24
2
446 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to union three related tables in one flat file? 8 48
(sql serv16)ssis 2016 question/check 1 74
SQL Query 2 34
SQL server 2014 replication error 35 45
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

830 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