Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Optional Date Range Parameters
birtnewbie2014
<p>Hi All! Forgive me if this is already posted somewhere but I searched and searched and couldn't find the answer to what I'm trying to do. What I have is a simple date range (From Date and To Date). In the select portion of the SQL I have defined the date as to_char(doc_dt, 'MM/DD/YYYY') and have it set up in the where clause as "to_char(doc_dt, 'MM/DD/YYYY') between ? and ?". I did the to_char here because I want the user to be able to enter their desired date into the parameter field in that format. (If there's a better way to do that, please let me know!) So the issue I am having is that both of those dates are optional and if the user decides to leave them both blank then I want to return all of the data. Right now, if I leave them both blank I don't get any data back. Any ideas on how to handle this?? Thanks so much for any advice you can provide!</p>
Find more posts tagged with
Comments
mwilliams
You could leave them both as not required. Then, in your dataset query, don't do the where date between ? and ?, just do the basic query. In the beforeOpen script of your dataset, you can check your parameters for values. If they are blank/null, you would leave the query as is. If a value was put in, you could add to your query from the script with something like:<br><br>this.queryText = this.queryText + " where date between '" + params["from"] + "' and '" params["to"] + "'";<br><br>Hope this helps. Let me know if I'm misunderstanding your issue.
birtnewbie2014
<p>Ok, I've removed the where date between ? and ? statement from my query. I'm not sure how to check the parameters for values or where to add the <strong>this.queryText </strong>line. I've only been using this tool for about a month so I'm not familiar with all of the ins and outs of it yet. Thanks for your help!</p>
pricher
<p>Hi,</p>
<p> </p>
<p>To modify the query before it is run, you need to write a few lines of JavaScript code in the beforeOpen method of the DataSet. To get to the beforeOpen method: 1) select your data set in the Data Explorer; 2) click on the Script tab at the bottom of your report design, and 3) select beforeOpen from the drop-down list above the script editor.</p>
<p> </p>
<p>In the attached example, you can see one example of how to modify the query. Here, I test to see if the parameter values are null. If they are not, I replace a dummy where clause ("where 1=1") with a where clause that will filter by date.</p>
<p> </p>
<p>Hope this helps,</p>
<p> </p>
<p>P.</p>
birtnewbie2014
<p>Good Morning P!</p>
<p> </p>
<p>Thanks for your help. Unfortunately, I cannot open the file you attached as I'm only allowed to use version 2.5.2. Can you just copy and paste the example that you were trying to show me? Or do you know of a way to open a newer file in 2.5.2? </p>
<p> </p>
<p>Thanks so much!</p>
pricher
<p>Hi,</p>
<p> </p>
<p>Here's the script added to the beforeOpen method of the data set:</p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">if (params["dateFrom"].value != null && params["dateTo"].value != null) {<br>
this.queryText = this.queryText.replace("where 1=1", "where o.orderdate between '" + params["dateFrom"].value + "' and '" + params["dateTo"].value + "'")<br>
}</span></p>
<p> </p>
<p>And here's the query:</p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">select count(o.ordernumber) as cnt<br>
, o.orderdate<br>
from orders o<br>
where 1=1<br>
group by o.orderdate</span></p>
<p> </p>
<p>Hope this helps,</p>
<p> </p>
<p>P.</p>
birtnewbie2014
<p>Bad news....I tried the above suggestion and it's still not working. I'm still having the problem where if I leave both parameters (From Date and To Date) blank, it doesn't return any data. I want it to return everything when those parameters are blank. Neither are marked as required. Any other suggestions/ideas??</p>
pricher
<p>Can you post your design?</p>
<p> </p>
<p>P.</p>
mwilliams
This design is made in 2.5.2 and works whether both, one, or neither of the parameters are entered. Hope it helps.
birtnewbie2014
<p><span style="font-family:'courier new', courier, monospace;">Thank you so much for your help. I have added the code in the beforeOpen script but I keep getting an error.</span></p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">Table (id=9): An exception occured during procession. Please see following message for details:</span></p>
<p><span style="font-family:'courier new', courier, monospace;">Failed to prepare the query execution for the date set: Test Date</span></p>
<p><span style="font-family:'courier new', courier, monospace;">Cannot get the result set metadata.</span></p>
<p><span style="font-family:'courier new', courier, monospace;">SQL statement does not return a ResultSet object.</span></p>
<p><span style="font-family:'courier new', courier, monospace;">SQL error #1: ORA-00933: SQL command not properly ended.... I would copy the whole error but I am unable to since it's on a classified machine. </span></p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">I know this is directly related to the code in the beforeOpen script since the error goes away when I remove it. </span></p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;"><strong>Here is what I have in the beforeOpen script</strong> -</span></p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">if (params["From Date"].value != null && params["To Date"].value != null){</span></p>
<p><span style="font-family:'courier new', courier, monospace;">this.queryText = this.queryText + " where doc_date between '" + params["From Date"] + "' and '" + params["To Date"] + "'";</span></p>
<p><span style="font-family:'courier new', courier, monospace;">}</span></p>
<p><span style="font-family:'courier new', courier, monospace;">else if(params["To Date"].value != null){</span></p>
<p><span style="font-family:'courier new', courier, monospace;">this.queryText = this.queryText + " where doc_date < '" + params["To Date"] + "'";</span></p>
<p><span style="font-family:'courier new', courier, monospace;">}</span></p>
<p><span style="font-family:'courier new', courier, monospace;">else if(params["From Date"].value != null){</span></p>
<p><span style="font-family:'courier new', courier, monospace;">this.queryText = this.queryText + " where doc_date > '" + params["From Date"] + "'";</span></p>
<p><span style="font-family:'courier new', courier, monospace;">}</span></p>
<p><span style="font-family:'courier new', courier, monospace;">else{</span></p>
<p><span style="font-family:'courier new', courier, monospace;">}</span></p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;"><strong>Here is what you have in the beforeOpen script</strong> - </span></p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">if (params["sDate"].value != null && params["eDate"].value != null){<br>
this.queryText = this.queryText + " where orderdate between '" + params["sDate"] + "' and '" + params["eDate"] + "'";<br>
}<br>
else if(params["eDate"].value != null){<br>
this.queryText = this.queryText + " where orderdate < '" + params["eDate"] + "'";<br>
}<br>
else if(params["sDate"].value != null){<br>
this.queryText = this.queryText + " where orderdate > '" + params["sDate"] + "'";<br>
}<br>
else{<br>
}</span></p>
<p> </p>
<p>Am I missing something??</p>
mwilliams
Does the report that I attached work for you though?<br><br>In your report, what is your original query?
birtnewbie2014
<p>Yes, I was able to run the sample report you provided. </p>
<p> </p>
<p>SELECT</p>
<p>d.doc_date,</p>
<p>d.doc_num,</p>
<p>l.status,</p>
<p>substr(ID,12,4) FY,</p>
<p>substr(DEPT,7,2) DEPT,</p>
<p>substr(Code,11,4) CODE,</p>
<p>substr(ID,7,2) AcctNum,</p>
<p>l.amt,</p>
<p>l.org_id,</p>
<p>l.comments</p>
<p>FROM</p>
<p>doc_tbl d,</p>
<p>doc_ln l,</p>
<p>corp_tbl f</p>
<p>WHERE d.uid = l.line_id</p>
<p>and f.uid = l.fid</p>
<p>and substr(ID,7,2) like ? <strong>(I have an optional AcctNum parameter)</strong></p>
<p>and substr(ID,12,4) like ? <strong>(I have an optional FY parameter)</strong></p>
<p>and substr(DEPT,7,2) like ? <strong>(I have an optional DEPT parameter)</strong></p>
<p>and l.status like ? <strong>(I have an optional Status parameter)</strong></p>
<p>ORDER BY d.doc_num</p>
mwilliams
Okay. Remove the ORDER BY d.doc_num from your original query and add that in your beforeOpen script (see below). Also, you already have a where statement started, so you don't need to say where again. Try this:<br><br>if (params["From Date"].value != null && params["To Date"].value != null){<br>this.queryText = this.queryText + " and doc_date between '" + params["From Date"] + "' and '" + params["To Date"] + "' ORDER BY d.doc_num";<br>}<br>else if(params["To Date"].value != null){<br>this.queryText = this.queryText + " and doc_date < '" + params["To Date"] + "' ORDER BY d.doc_num";<br>}<br>else if(params["From Date"].value != null){<br>this.queryText = this.queryText + " and doc_date > '" + params["From Date"] + "' ORDER BY d.doc_num";<br>}<br>else{<br>this.queryText = this.queryText + " ORDER BY d.doc_num"<br>}
birtnewbie2014
<p>So close! I'm still having the issue of if I leave either date parameters blank I don't get any data back (instead of selecting the radio button for Null Value, I select the radio button beside the input field and do not enter a date). The reason I need to do this is b/c the users will be running this report in another program (Momentum) and they need to be able to leave these fields completely blank.</p>
<p> </p>
<p>Thanks!!</p>
pricher
<p>Be careful: a null value is not the same as an empty value!</p>
<p> </p>
<p>You will need to test for both scenarios in your script. The following IF statement should work:</p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">if (params["From Date"].value != null && params["To Date"].value != null) && (params["From Date"].value != "" && params["To Date"].value != "")</span></p>
<p> </p>
<p>Hope this helps,</p>
<p> </p>
<p>P.</p>
birtnewbie2014
<p>That did it!!! Thanks so much for all of your help guys!! I'm sure I'll be back...
</p>
mwilliams
We're always glad to help. Definitely let us know whenever you have questions.