I am trying to convert the following SQL Server expression into a visibility expression in SSRS for a certain textbox that contains an error message:
select address, commonPlaceName, municipality where #Location not like '$[0-9]%'
My SSRS report lets users search for #Location, which is a combination of the address and commonPlaceName and returns a table of records.
I am trying to limit the number of rows returned based on both the character length and whether the parameter value supplied contains a number.I want to show an error message when they search for a common place or address without a number that says "Sorry your search was too broad so the number of records has been limited".
I got the length part to work by using the following:
=iif(len(Parameters!Location.Value)<=5,false,true)
But I can't figure out how to search for a number in a string
For those interested, in the SQL script, I am setting the rowcount based on a case statement that parallels the visibility options I am trying to set in SSRS.
I am still learning SSRS so any help is much appreciated!
EDIT:
Below is the SQL statement I am using to limit rowcount if that helps:
declare #top int
set #top = case when len(#Location) <= 5 then 100
when #Location not like '%[0-9]%' then 100
when len(#Location) >5 then 0
end
set rowcount #top
There are a number of ways to do this.
Calculate the visibility in your query and set the Visibility to the calculated query field. This is cleaner in the report. Normally you would want to put all display logic in the report itself, however, I have been lazy a time or two.
=IIF(Fields!ShouldShowField.Value==1,0,1)
Use SSRS functions such as InStr() or
=IIF(Regex.Replace(Fields!MyField.Value, "[MyRegExString]").Length == Fields!MyField.Value.Length, 0, 1)
Use Custom Code for advanced string manipulation and call your code similar to
=IIF(Code.ShouldShowTextBox(Fields!MyField.Value),0,1)
Okay I think I found a workaround for now. I ended up creating a new dataset:
select
'LocationWithNum' = case
when #Location like '%[0-9]%' then 'true'
else 'false'
end
Then I changed the visibility expression of the error message textbox to the following:
=iif(len(Parameters!Location.Value)<=5 or First(Fields!LocationWithNum.Value, "LocationWithNum") = false, false, true)
So far this seems to work well. Let me know if you guys think of anything else!
Related
I am trying to get CSV IDs from a table from sql server and assign the result to a variable. below is the sql I have put inside the Execute SQL Task
set nocount on
declare #csv varchar(max) = ''
select #csv = #csv + cast(companyid as varchar(10)) + ',' from company where isprocessed = 0
select substring(#csv,1,len(#csv) - 1) as companyids
As you can see its a simple and standard way of getting csv of a field in t-sql. It works perfectly in query window but throwing below error when I run the Task in SSIS 2012
[Execute SQL Task] Error: The value type (__ComObject) can only be
converted to variables of type Object.
[Execute SQL Task] Error: An error occurred while assigning a value to
variable "sCSVCompanyIds": "The type of the value (DBNull) being
assigned to variable "User::sCSVCompanyIds" differs from the current
variable type (String). Variables may not change type during
execution. Variable types are strict, except for variables of type
Object. ".
Below are the settings of Execute SQL Task
In General tab, Resultset project is set to single row
In Result Set tab, Result Name is set to 0 (I also tried by setting it to csvids which is the alias column name in the select list) and Variable Name is set to User::sCSVCompanyIds
I have no clue why its not working. After wasting so much time I am worked out a hard way which by returning the result as Full Row set (same SQL which returns 1 row 1 column always) and add a for each loop container to loop throw the result set (which will always iterates only once for obvious reasons) and assign the result set's fields to the variable. It works for me but there should be easy way of doing it. What I am missing?
The problem is with the nvarchar(max) data type. I assume it will be same for varchar(max) also. Though relevant data types in SSIS are DT_NTEXT and DT_TEXT are present for some reason we are getting this error.
There are multiple options to handle this.
One Of course there might be more appropriate way to handle this by cast/converting the column within the query to fixed length instead of max something like cast(myvarcharmaxfield as varchar(8000)). In my case it doesn't work because I expect bigger length string. I am generating a csv string from a unique identifier column which itself is 36 chars long string and needs 3 extra chars for quoting them with signle quote and a comma as separator which will support only 205 values. So it doesn't work for me.
So I left with no option but to stick the way I implemented already which is in my question
After wasting so much time I am worked out a hard way which by
returning the result as Full Row set (same SQL which returns 1 row 1
column always) and add a for each loop container to loop throw the
result set (which will always iterates only once for obvious reasons)
and assign the result set's fields to the variable
The new way I learnt is from an unaccepted answer from this question
Change the Oledb connection to ADO.NET connection
I think this is fair enough but it requires me to create another connection manager (so that I don't have to change all existing tasks) and use it for this type of taks where I need csv but I did not buy it as I have very little time to doing research on creating connection string of it which appears to me with the type of erros I am getting is different from oledb.
I would like to create a column that displays if a student has passed or failed. To pass the overall grade needs to be above 40.
I have done this before using access using the following:
IIf([Overall]<=40,"fail","pass")
But I’m very new to visual basic. Anyone got any ideas on how I can do this?
The IDE I am using is Visual Studio
This is the table
Or if there are any tutorials that you would recommend that would be great.
You can do it in your table definition:
You can use a CASE WHEN statement to check for a criteria and provide a value.
You can not refer to a computed column in another computed column.
So you can add a column like this:
[ColumnName] AS CASE WHEN (the formula for overall) <= 40 THEN 'fail' ELSE 'pass' END
Also you can make the result as a bit (boolean) data type and let the application decide to show a string instead of true or false.
I'm working on SSRS report. I have Dataset that returns below values.
Now I wanted to change color of Data field on bases of this bit.
I tried below, but no success. It always shows False in report.
=IIF(CBool(Fields!IsAuthorized.Value)=True,"Blue","Black")
=IIF(CInt(Fields!IsAuthorized.Value)=1,"Blue","Black")
=IIF(Fields!IsAuthorized.Value=1,"Blue","Black")
Below Image show IsAuthorized.Value on data field.
Please help me!
Have a look here..
http://www.sqlservercentral.com/Forums/Topic1459800-1063-1.aspx
For example:
=IIF( Fields!IsState.Value <> 0 and Fields!RegionID.Value <>"12","Yes","No")
So yours should be something like:
=IIF( Fields!IsAuthorized.Value <> 0 ,Black,Blue)
This should cover it.
Issue was with while I was passing the value to Dataset to fetch rows.
SELECT DISTINCT Number,
[dbo].[Function](#UserID) AS IsAuthorized,
FROM TableName
Here, #UserID always passed as blank from my web application and this is why conversion gets failed.
I changed my below line of code in web app to set it's correct value.
Before
ReportParams.Add(new ReportParameter("UserId", Request.QueryString["UserId"].ToString()));
After
ReportParams.Add(new ReportParameter("UserID", Request.QueryString["UserID"].ToString()));
Also, I have set Default values for #UserID in Parameters properties as blank. This is why also it was causing issue.
I know there are existing questions regarding to this error. but my situation looks different!
I'm creating a SSRS2012 report that can manually input UserIDs number (such as 15, 130, etc..) as the IC_ID. I want to ensure when input is null, report can display all IC_ID. I tried two method to approach it.
Data source: Oracle Sql Developer, table:IC, column: IC_ID datatype:NUMBER
Parameter: UserIDs, datatype: Integer, allow null value.
------------Method 1:---------------
SSRS dataset query, I've tried:
select * from IC
where IC_ID=to_number(COALESCE(:UserIDs, IC_ID))
select * from IC
where IC_ID=to_char(COALESCE(:UserIDs, IC_ID))
select * from IC
where IC_ID=COALESCE(:UserIDs, IC_ID)
But all failed with same error: NUMBERORA-00932 expected CHAR got NUMBER. When I run them in Oracle database, show same error as above. So I guess the problem is how to make data type consistent, column IC_ID data type is NUMBER, if I set parameter UserIDs as Integer, why still not consistent?
PS:when I only use query as:
**select * from IC where IC_ID=(:UserIDs)**
no matter I set parameter datatype to text or integer, the report works fine and display info of whatever IC_ID value I manually input. But it didn't show data when parameter is null!
---------Method 2---------
SSRS dataset query, I've tried:
select * from IC
where IC_ID=(:UserIDs) or (:UserIDs) IS NULL
failed with error: attempted to read or write protected memory. this is often an indication that other memory is corrupt. When I run this query in Oracle, it run well with not error, no matter I pass a number or null to (:UserIDs). So I guess the problem is how to use (:UserIDs) TWICE in SSRS dataset query.
I'm very sure it's not because of memory limit, because this report run well displaying all IC_ID info when there is no parameter at all. It seems it doesn't allow one parameter to show up more than once in dataset query, whenever I tried something like: select * from IC where IC_ID=(:UserIDs) AND IC_SubID=(:UserIDs), it will fail with memory corrupt error.
But it works fine if (:UserIDs) only show up once in query.
---------Method 3----------------
Some posts said in order to display all info when parameter is null, we can add a list of all IC_ID as the default value of parameter. I don't think it works for this case, because my dataset query is actually very complex, starting with a long cte. It will be too complex and slow to add this whole cte query into the list as default value.
I would appreciate if anyone could give some help of how to solve any of above errors, or tell me other ways to display all info while parameter is null!
Thank you
Isn't possible to cast the parameter as an integer? Like cast((:UserIDs as int)) (or maybe cast as a char). Don't know if this helps but i would expect that the datatype is always the same.
Or you could maybe use
(:UserIDs) in (case when (:UserIDs) is null then (select UserIDs from table) else (:UserIDs) end)
I have a SQL Server engine in which I have a filed with filter clause. I need that clause to be compose to show all rows except those that contain :, -, ~.
My query is:
SELECT 1
WHERE '' LIKE '%[^:-~]%'
It is not working - it shows zero rows. Also I try this:
SELECT 1
WHERE 'aa:a' LIKE '%[^:-~]%'
And it shows as result 1, which is not desired result.
Is there a way to manage this?
REMARK: expression after like must be string which will be saved inside the table field (for exmaple: '%[^:-~]%' will be used as LIKE x.fldFilter)
EDIT: I need to make validation inside my engine inside SQL Server. I have a table with Parameters. Inside it I have column Format. For a specific parameter I need to check if value provided fits Format column.
For example:
DECLARE #value AS VARCHAR(1000) = 'aaa:aa';
SELECT 1 FROM dbo.ParameterDefinitions X WHERE #value LIKE X.[Format];
Where X.[Format] column contains '%[^:-~]%'.
When I test a value check must return 1 if it fits conditions and nothing if not fits.
So if I test value 'aaa:aa' or even ' ' it works. But when I have empty string ('') the condition does not working.
Unfortunately I can not change my engine and can not replace '' with space for example. I just wonder why '' does not fit the condition?
This is due to SQL Server not having a solid regex implementation.
Instead of negating your search with ^
Negate it with Not
SELECT 1
WHERE '' NOT LIKE '%[:-~]%'
Returns 1 row
SELECT 1
WHERE 'aa:a' NOT LIKE '%[:-~]%'
Returns 0 rows
EDIT:
Breaking down your search cases
'' LIKE '%[^:-~]%'
[^:-~] requires a single character so an empty string must fail
'aa:a' LIKE '%[^:-~]%'
% is a 0 or more wildcard which lets [^:-~] take its choice of 'a' while either % collects your forbidden character.
With a full regex engine we could repeat your negated range with the following [^:-~]* but SQL Server doesn't support that. Docs
The only option left to us is to perform a search for the forbidden characters '%[:-~]%' and to negate the like.