SSRS load report even if cascading parameter is empty - sql-server

I have cascading parameters like: Year > Company After choosing Year value, Company parameters are refreshed.
So for example If I choose 2016, but at this year no data, Company parameter is empty (no values) and report says that parameter can't be blank (I've checked "Allow blank values """, but It not working with multivalued parameters) See image below:
How can I load report with message like: "Sorry, there is no data"? Or provide any default value for Company parameter if there is no other values? Have you any ideas?

One solution is to make your Year parameter data-driven, from the same table as your companies. Make a new dataset like the one below, and use it as the available values for your Year parameter.
SELECT DISTINCT year
FROM companies
WHERE report_relevant = 'Yes'
ORDER BY 1
This limits the users to only pick years where valid companies exist. As soon as a 2016 company is added to your data, the report will automatically include 2016 as a Year parameter option.

You could use a UNION to add an extra row of data to your parameter, but Count() your valid companies so the extra row is only included when no other companies would be.
SELECT company_id, company_name
FROM companies
WHERE year = #Year
UNION
SELECT '-1', 'No companies in ' + #Year
WHERE (SELECT Count(company_id) FROM companies WHERE YEAR = #Year) = 0

Related

Creating SQL views in VBA

I am trying to create a view on a table called petients in my database. The table has five columns. One of them is the column which I want to keep patient admitted date. It data type is datetime so I want to create a query that filters the data in this table based on current date. For example I want create a view that shows only details of petients who have been recorded on the current day.
Here is my code:
CREATE VIEW [dbo].[recent petients]
AS
SELECT petient_id, name, age, contact
FROM [petients]
WHERE [date] = 'date.Today'
I am getting an error saying that failed to convert date to string. Can you help me to solve it, or where is my code wrong?
Your code looks like SQL Server code. If so, I would recommend:
SELECT petient_id, name, age, contact
FROM [patients]
WHERE [date] = CONVERT(date, GETDATE());
As a note: This version is much better than DATEDIFF() because it allows the use of an index on patient([date]).
If the "date" column has a time component, you can use:
WHERE CONVERT(date, [date]) = CONVERT(date, GETDATE())
Note that this is also index-safe in SQL Server.
I'm assuming you are using Transact-SQL from Microsoft SQL Server, but you should specify the sql dialect you are using.
Since the datetime field type generally includes also a time, it is better to use the DATEDIFF function: https://learn.microsoft.com/it-it/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15
In your case, to consider only the record where date=today, the difference in days must be zero:
--SQL QUERY
WHERE DATEDIFF(day, GETDATE(), [date]) = 0
day identifies the element you want to consider the difference. A list of names or abbreviations can be found in the link
GETDATE() returns now datetime
2nd and 3rd arguments are the dates you want to make the difference between

Setting a Default SSRS parameter

Default SSRS Settings. I have several parameters, some are interdependent on each other.
For example:
There is season parameter, final, and then i have a user parameter.
The final field is Yes or No.
The user output depends on the year
If my year is 2019, i will only see the users that are active for that year.
select distinct user where year = #year
I need to set defaults:
season needs to be max(year) how do i select max possible value?
final, options are yes or no, on the server we select 'N'
to be default. (see graphic below)
But with the users we want to select 'Select All' as a default based on the year. I don't know how to get the default to be a 'select all' possible options based on the query/year.
You can not achieve this from report manager unless it is defined in the report.
E.g. for season, you need to define the default from a dataset, and create a dataset with the SQL to SELECT MAX(year).

SSRS data driven subscription email to : parameter dependent on report parameter?

Hi I am using visual studio 2015 to make an SSRS 2014 report.
The place I am stuck at is my email recipient is dependent on what company is selected from the SSRS report.
Not that it should matter but my report parameters are Company
(really companyID),begdat,enddate.
select email from users where companyid= #companyid<< which would be my selection.
Most examples I see have a defined list of emails or just 1 person to email. I want to send a report per company only to the users of that company aka dynamic recipient list.
Modify your query as: select email, companyid from users
At the next step in select an option Get the value from the database for the TO field and set it to email.
At the next step change your company option to Get the value from the database and set it to companyid.
Then users from company CompanyA will receive only a CompanyA report, and CompanyB - only a CompanyB report.
You could debug the subscription using temporary query with only your email:
select email='your#email', companyid = 'CompanyAid'
UNION ALL
select email='your#email', companyid = 'CompanyBid'

Date range based on Column Date

I am using the latest SQL Server. I have a table with a CreatedDate column. I need to write a Query that uses dates that are plus or minus 7 from the Date in CreatedDate. I have no clue how to go about this. My thought was this:
DECLARE #Date datetime
DECLARE #SevenBefore datetime
DECLARE #SevenAfter datetime
SET #Date = CreatedDate
SET #SevenBefore = DATEADD(day,-7,#Date)
SET #SevenAfter = DATEADD(day,7,#Date)
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
The issue with this is that I cannot use "CreatedDate" as a SET #DATE because SQL gives an error "Invalid column name 'CreatedDate'"
Any help would be appreciated. I cannot list a date because every date in that column could be different.
Thanks
In this case, you need to stop thinking as a programmer would, and start thinking as a Database programmer would.
Lets work only with this central part of your query:
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
Now, you say that the CreatedDate is a column in a table. For this example, I will assume that the CreatedDate is in a table other than the one in your example above. For this purpose, I will give two fake names to the tables. The table with the CreatedDate, I will call tblCreated, and the one from the query above I will call tblData.
Looking above, it's pretty obvious that you can't compare an entire table row to a date. There must be a field in that table that contains a date/time value. I will call this column TargetDate.
Given these assumptions, your query would look something like:
SELECT *
FROM tblCreated tc
INNER JOIN tblData td
ON td.TargetDate BETWEEN DATEADD(day, -7, tc.CreatedDate) and DATEADD(day, 7, tc.CreatedDate)
Looking at this, it is clear that you still need some other associations between the tables. Do you only want all data rows per customer based on the Created date, or perhaps only want Creations where some work was done on them as shown in the Data records, or ??. Without a fuller specification, we can't help with that, though.

Running a select query where using 'LIKE' and 'ISNULL' doesn't give me the expected result in MS SQL Server

I have the following table in MS SQL Server:
Value CustomerName Date
-------------------------------------------------------------------
1 Sam[Sammy] 2016-09-30 11:13:15.550
2 Rimi 2016-09-30 11:13:15.550
I am running the following query to fetch the data:
SELECT *
FROM Table
WHERE CONVERT(varchar, convert(date,BillDate), 112) = '20160930' AND
CustomerName LIKE isnull(NULL,CustomerName)
We have a Customer search form that has a Date field and a Customer Name field.The user has the option to search using the customer name but that's optional.
The above query is used to search for the customers using the Date field only.
Now, when we run the query to search for customers it does not come up with the expected result. The CustomerName having the box brackets '[]' in their names are missing.
We cannot remove the box brackets in the names.
Is there a way to sort out this problem ?

Resources