Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Filtering Date (Show All Records)

Posted on 2013-12-24
2
Medium Priority
?
481 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

876 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