Rdlc report alignment when it filed is empty or null - rdlc

I am working with Rdlc report.
I have these fields:
First name
Address1
Address2
City, state, zip
If any one of these fields is empty, it should not show the blank space.
For example (expected output)
First name,
Address1,
City, state, zip
But, as show in the above image, I am getting this:
First name,
Address1,
........................<-(blankspace is showing here)
City, state, zip
I tried changing Visiblity -> Expression ->
=IIF(String.IsNullOrEmpty(Fields!Address2.Value), false,True)

I think that the expression with String.IsNullOrEmpty didn't works.
Try with one of this two options:
1.=IIF(IsNothing(Fields!Address2.Value),False,True)
2.=IIF(Len(Fields!Address2.Value) = 0,False,True)
According to the comments, the solution is to create a single textbox in which put two (or more) fields and concatenate the value if the second fields has a real value or is empty.
So the expression will be:
=Fields!Name.Value + System.Environment.NewLine + Fields!SAddr_Line1.Value + IIF(‌​Len(Fields!Address2.Value) = 0, "", System.Environment.NewLine + Fields!Address2.Value) + Fields!ShipTo.Value
For more readability:
=Fields!Name.Value
+ System.Environment.NewLine
+ Fields!SAddr_Line1.Value
+ IIF(‌​Len(Fields!Address2.Value) = 0, "", System.Environment.NewLine + Fields!Address2.Value)
+ Fields!ShipTo.Value

Just in case this helps somebody.
I suffered the same (and I had quite complex grouping on) until I did:
Fields!FieldName.Value * 1
(You can then convert to String using CStr() if needed)
And voilà!

You Can Use if else statement in the following cases in rdlc report
1=IIF(IsNothing(Fields!Amount.Value),False,True)
2.=IIF(Len(Fields!Amount.Value) = 0,False,True)
=iif(IsNothing(Sum(Fields!Amount.Value)),0.00,True).ToString()
last one when no records found and display 0 its working!

Related

MS Access Query: Use a passed parameter for a list of records or the parameter " ALL" to generate the whole list

I'm trying to pass a parameter to my query in the criteria field. The parameter is contained on a form in the combobox cboReportSender. cboReportSender contains a list of departments that I run reports for. Also contained in the list is " ALL". When this is selected, I wish the report to display all records. I'm sure that the query is looking for a field literally containing "Like *"
Am I going at this from a wrong angle?
IIf([Forms]![frmRunReport]![cboReportSender]=Trim(" ALL"),"Like *",[Forms]![frmRunReport]![cboReportSender])
Firstly, I wondered whether your combobox selected a numerical value or an alphanumerical value. But when I created a database for testing, neither worked for me in combination with the operator Like * . I needed to typ Like '\*' , with a single inverted comma before and after the asterisk. Even better, adding this inverted comma's works with both numerical and alphanumerical values.
The second thing I needed to change was, to add a second like-operator. In the end, your criterion would become :
IIf([Forms]![frmRunReport]![cboReportSender]=Trim(" ALL"),"Like '*'","Like '" & [Forms]![frmRunReport]![cboReportSender] & "'")
Consider returning the field itself when combobox selects ALL.
SELECT ...
FROM ...
WHERE myDepartmentField = IIf(
TRIM([Forms]![frmRunReport]![cboReportSender]) = 'ALL',
myDepartmentField,
TRIM([Forms]![frmRunReport]![cboReportSender])
)
And if combobox is empty, use NZ to default to field itself.
WHERE myDepartmentField = IIf(
TRIM([Forms]![frmRunReport]![cboReportSender]) = 'ALL',
myDepartmentField,
TRIM(
NZ(
[Forms]![frmRunReport]![cboReportSender],
myDepartmentField
)
)

Concatenate inside SELECT CASE

I am moving a customer database which has a First Name, Last Name and Company Name. The current data will either have a First & Last Name or a Company name
In the new database I have a "Print Name" field which I need to combine the first and last name into, or if there is a company name present I need to use this value.
I have tried the bellow CASE WHEN expression to no avail.
SELECT
fstnam,
lstnam,
cmpnam,
CASE
WHEN csttbl.cmpnam = null THEN fstnam||' '||lstnam
ELSE csttbl.cmpnam
END as PrintName
FROM csttbl;
With the above query I only have a company name return in the print name column, not the combined First & Last Name. the returned data can be seen here https://drive.google.com/open?id=0B31ZRmFSX6rMYmNOczlRNE1lM2c
Any help would be appreciated
Change
csttbl.cmpnam = null
To
csttbl.cmpnam is null
RDBMS cannot compare null to any value, so using = with null is of no use. To compare nulls you should use is null which works on almost all RDBMS.
I would use coalesce():
SELECT fstnam, lstnam, cmpnam,
COALESCE(csttbl.cmpnam, fstnam|| ' ' || lstnam) as PrintName
FROM csttbl;
Didn't think of just concatonating the whole string as there is no First & Last name if there is a company name. COALESCE doesn't seem to work for me though so did the below
SELECT
fstnam,
lstnam,
cmpnam,
csttbl.cmpnam||fstnam|| ' ' ||lstnam as PrintName
FROM csttbl;

SQL Pull apart a string at specific points (based off static text strings)

I have a string returning with some static text to denote where the next element will be displayed. It's basically a concatenated field made of a bunch of label+text values that had no other place to live in the schema in this database.
The output would look like
Product: productname Company: productcompany Type: producttype Site: productsite
Any of these could be blanks, but the "labels" of Product:/Company:/Type:/Site: will always exist. I'd like to rip apart the string so I can create columns with each.
I can do substrings on here I believe, but I haven't had any success pulling apart the string correctly.
Some things I've tried with no success!
select
impl_nm,
instr(impl_nm, 'Product:', 1,1)+1 as Start,
instr(impl_nm, 'Company:', 1, 2) as End
--substr(val, instr(impl_nm, 'Product: ', 1,1) + 1, decode(instr(impl_nm, 'Company:',1,2),0,length(impl_nm)+1,instr(impl_nm, 'Company:',1,2) ) - instr(impl_nm, 'Product: ', 1,1)-1 )
from products
I've had to do something similar with string manipulation - to redirect a subscription at generation time.
Having decided not to drop the entire scan through by flags - the basic solution is:
Select right(left(impl_nm,Charindex( 'Company:',impl_nm, 1)-1),len(charindex( 'Company:',impl_nm, 1))-Charindex(left(impl_nm,Charindex('Company:',impl_nm, 1)), 'Product:',1) +len('Product:')+1)
,left(impl_nm,Charindex( 'Company:',impl_nm, 1)-1)
from (select 'Product: Sometext Company: somemoretext' impl_nm) c
Basically Truncate the string down to the end of the next field Name "Company" and then take the rightmost part corresponding to the length of the field.

SQL - Conditionally joining two columns in same table into one

I am working with a table that contains two versions of stored information. To simplify it, one column contains the old description of a file run while another column contains the updated standard for displaying ran files. It gets more complicated in that the older column can have multiple standards within itself. The table:
Old Column New Column
Desc: LGX/101/rpt null
null Home
Print: LGX/234/rpt null
null Print
null Page
I need to combine the two columns into one, but I also need to delete the "Print: " and "Desc: " string from the beginning of the old column values. Any suggestions? Let me know if/when I'm forgetting something you need to know!
(I am writing in Cache SQL, but I'd just like a general approach to my problem, I can figure out the specifics past that.)
EDIT: the condition is that if substr(oldcol,1,5) = 'desc: ' then substr(oldcol,6)
else if substr(oldcol,1,6) = 'print: ' then substr(oldcol,7) etc. So as to take out the "desc: " and the "print: " to sanitize the data somewhat.
EDIT2: I want to make the table look like this:
Col
LGX/101/rpt
Home
LGX/234/rpt
Print
Page
It's difficult to understand what you are looking for exactly. Does the above represent before/after, or both columns that need combining/merging.
My guess is that COALESCE might be able to help you. It takes a bunch of parameters and returns the first non NULL.
It looks like you're wanting to grab values from new if old is NULL and old if new is null. To do that you can use a case statement in your SQL. I know CASE statements are supported by MySQL, I'm not sure if they'll help you here.
SELECT (CASE WHEN old_col IS NULL THEN new_col ELSE old_col END) as val FROM table_name
This will grab new_col if old_col is NULL, otherwise it will grab old_col.
You can remove the Print: and Desc: by using a combination of CharIndex and Substring functions. Here it goes
SELECT CASE WHEN CHARINDEX(':',COALESCE(OldCol,NewCol)) > 0 THEN
SUBSTRING(COALESCE(OldCol,NewCol),CHARINDEX(':',COALESCE(OldCol,NewCol))+1,8000)
ELSE
COALESCE(OldCol,NewCol)
END AS Newcolvalue
FROM [SchemaName].[TableName]
The Charindex gives the position of the character/string you are searching for.
So you get the position of ":" in the computed column(Coalesce part) and pass that value to the substring function. Then add +1 to the position which indicates the substring function to get the part after the ":". Now you have a string without "Desc:" and "Print:".
Hope this helps.

How to Replace Multiple Characters in Access SQL?

I'm a novice at SQL, so hopefully someone can spell this out for me. I tried following the "Replace Multiple Strings in SQL Query" posting, but I got stuck.
I'm trying to do the same thing as the originator of the above posting but with a different table and different fields. Let's say that the following field "ShiptoPlant" in table "BTST" has three records (my table actually has thousands of records)...
Table Name: BTST
---------------
| ShiptoPlant |
| ----------- |
| Plant #1 |
| Plant - 2 |
| Plant/3 |
---------------
Here's what I'm trying to type in the SQL screen:
SELECT CASE WHEN ShipToPlant IN ("#", "-", "/") Then ""
ELSE ShipToPlant END FROM BTST;
I keep getting the message (Error 3075)...
"Syntax error (missing operator) in query expression
'CASE WHEN ShiptoPlant IN (";","/"," ") Then "" ELSE ShipToPlant END'."
I want to do this operation for every character on the keyboard, with exception of "*" since it is a wildcard.
Any help you could provide would be greatly appreciated!
EDIT: Background Information added from the comments
I have collected line-item invoice-level data from each our 14 suppliers for the 2008 calendar year. I am trying to normalize the plant names that are given to us by our suppliers.
Each supplier can call a plant by a different name e.g.
Signode Service on our master list could be called by suppliers
Signode Service
Signode - Service.
SignodeSvc
SignodeService
I'm trying to strip non-alphanumeric chars so that I can try to identify the plant using our master listing by creating a series of links that look at the first 10 char, if no match, 8 char, 6, 4...
My basic hang-up is that I don't know how to strip the alphanumeric characters from the table. I'll be doing this operation on several columns, but I planned on creating separate queries to edit the other columns.
Perhaps I need to do a mass update query that strips all the alphanumerics. I'm still unclear on how to write it. Here's what I started out with to take out all the spaces. It worked great, but failed when I tried to nest the replace
UPDATE BTST SET ShipToPlant = replace(ShipToPlant," ","");
EDIT 2: Further Information taken from Comments
Every month, up to 100 new permutations of our plant names appear in our line item invoice data- this could represent thousands of invoice records. I'm trying to construct a quick and dirty way to assign a master_id of the definitive name to each plant name permutation. The best way I can see to do so is to look at the plant, address, city and state fields, but the problem with this is that these fields have various permutations as well, for example,
128 Brookview Drive
128 Brookview Lane
By taking out alphanumerics and doing
LEFT(PlantName,#chars) & _
LEFT(Address,#chars) & _
LEFT(City,#chars) & _
LEFT(State,#chars)
and by changing the number of characters until a match is found between the invoice data and the Master Plant Listing (both tables contain the Plant, Address, City and State fields), you can eventually find a match. Of course, when you start dwindling down the number of characters you are LEFTing, the accuracy becomes compromised. I've done this in excel and had decent yield. Can anyone recommend a better solution?
You may wish to consider a User Defined Function (UDF)
SELECT ShiptoPlant, CleanString([ShiptoPlant]) AS Clean
FROM Table
Function CleanString(strText)
Dim objRegEx As Object
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "[^a-z0-9]"
CleanString = objRegEx.Replace(strText, "")
End Function
You could use the built in Replace function within Access
SELECT
Replace(Replace(Replace(ShipToPlant, "#", ""), "-", ""), "/", "") AS ShipToPlant
FROM
BTST
As others have said, within Access you can write your own functions in VBA and use them in your queries.
EDIT:
Here's a way to handle the nested Replace limit by wrappering the Replace function within our own function. It feels dirty but it works- put this in a module within Access
Public Function SuperReplace(ByRef field As String, ByVal ReplaceString As String) As String
' Size this as big as you need... it is zero-based by default'
Dim ReplaceArray(3) As String
'Fill each element with the character you need to replace'
ReplaceArray(0) = "#"
ReplaceArray(1) = "-"
ReplaceArray(2) = "/"
ReplaceArray(3) = " "
Dim i As Integer
For i = LBound(ReplaceArray) To UBound(ReplaceArray)
field = Replace(field, ReplaceArray(i), ReplaceString)
Next i
SuperReplace = field
End Function
Then test it with this query
SELECT
SuperReplace(ShipToPlant,"") AS ShipToPlant
FROM
BTST
You might want to take this an expand it so that you can pass in an array of strings instead of hard-coding them into the function.
EDIT 2:
In response to the additional information in the comments on the question, here's a suggestion for how you might want to handle the situation differently. The advantage to this apprach is that once you have mapped in a plant name permutation, you won't need to perform a string replace on future data in future years, only add new plant names and permutations to the map.
Start with creating another table, let's call it plant_map
CREATE TABLE plant_map (id AUTOINCREMENT PRIMARY KEY, name TEXT, master_id LONG)
into plant_map, add all of the permutations for plant names and insert the id for the name you wish to use to refer to a particular plant name permutation group with, into the master_id field. From your comments, I'll use Signode Service
INSERT INTO plant_map(name, master_id) VALUES ("Signode Service", 1);
INSERT INTO plant_map(name, master_id) VALUES ("Signode Svc", 1);
INSERT INTO plant_map(name, master_id) VALUES ("Signode - Service", 1);
INSERT INTO plant_map(name, master_id) VALUES ("Signode svc", 1);
INSERT INTO plant_map(name, master_id) VALUES ("SignodeService", 1);
Now when you query BTST table, you can get data for Signode Service using
SELECT
field1,
field2
FROM
BTST source
INNER JOIN
(
plant_map map1
INNER JOIN
plant_map map2
ON map1.master_id = map2.id
)
ON source.ShipToPlant = map1.name
WHERE
map2.name = "Signode Service"
Data within table BTST can remain unchanged.
Essentially, this is joining on the plant name in BTST to the name in plant_map then, using master_id, self joining on id within plant_map so that you need only pass in one "common" name. I would advise putting an index on each of the columns name and master_id in plant_map as both fields will be used in joins.
Don't think Access supports the CASE statement. Consider using iif:
iif ( condition, value_if_true, value_if_false )
For this case you can use the REPLACE function:
SELECT
REPLACE(REPLACE(REPLACE(yourfield, '#', ''), '-', ''), '/', '')
as FieldName
FROM
....
Create a public function in a Code module.
Public Function StripChars(ByVal pStringtoStrip As Variant, ByVal pCharsToKeep As String) As String
Dim sChar As String
Dim sTemp As String
Dim iCtr As Integer
sTemp = ""
For iCtr = 1 To Len(pStringtoStrip)
sChar = Mid(pStringtoStrip, iCtr, 1)
If InStr(pCharsToKeep, sChar) > 0 Then
sTemp = sTemp & sChar
End If
Next
StripChars = sTemp
End Function
Then in your query
SELECT
StripChars(ShipToPlant, "abcdefghijklmnopqrstuvwxyz0123456789") AS ShipToPlantDisplay
FROM
BTST
Notes - this will be slow for lots of records - if you what this to be permanent then create an update query using the same function.
EDIT: to do an Update:
UPDATE BTST
SET ShipToPlant = StripChars(ShipToPlant, "abcdefghijklmnopqrstuvwxyz0123456789")
OK, your question has changed, so the solution will too. Here are two ways to do it. The quick and dirty way will only partially solve your issue because it won't be able to account for the more odd permutations like missing spaces or misspelled words. The quick and dirty way:
Create a new table - let's call it
tChar.
Put a text field in it - the
char(s) you want to replace - we'll
call it char for this example
Put all the char or char combinatios that you want removed in this table.
Create and run the query below.
Note that it will only remove one
item at a time, but you can also put
different versions of the same
replacement in it too like ' -' or
'-'
For this example I created a table called tPlant with a field called ShipToPlant.
SELECT tPlant.ShipToPlant, Replace([ShipToPlant],
(SELECT top 1 char
FROM tChar
WHERE instr(ShipToPlant,char)<>0 ORDER BY len(char) Desc),""
) AS New
FROM tPlant;
The better (but much more complex) way. This explanation is going to be general because it would be next to impossible to put the whole thing in here. If you want to contact me directly use my user name at gmail.:
Create a table of Qualifiers -
mistakes that people enter like svc
instead of service. Here you would
enter every wierd permutation you
get.
Create a table with QualifierID and
Plant ID. Here you would say which
qualifier goes to which plant.
Create a query that joins the two
and your table with mistaken plant
names in it. Use instr so say what
is in the fields.
Create a second query that
aggragates the first. Count the
instr field and use it as a score.
The entry with the highest score is
the plant.
You will have to hand enter the ones
it can't find, but pretty soon that
will be next to none as you have
more and more entries in the table.
ughh
You have a couple different choices. In Access there is no CASE in sql, you need to use IIF. It's not quite as elegant as the solutions in the more robust db engines and needs to be nested for this instance, but it will get the job done for you.
SELECT
iif(instr(ShipToPlant,"#")<>0,"",
iif(instr(ShipToPlant,"-")<>0,"",
iif(instr(ShipToPlant,"/")<>0,"",ShipToPlant ))) AS FieldName
FROM BTST;
You could also do it using the sql to limit your data.
SELECT YourID, nz(aBTST.ShipToPlant,"") AS ShipToPlant
FROM BTST LEFT JOIN (
SELECT YourID, ShipToPlant
FROM BTST
WHERE ShipToPlant NOT IN("#", "-", "/")
) as aBTST ON BTST.YourID=aBTST.YourID
If you know VB you can also create your own functions and put them in the queries...but that is another post. :)
HTH
SELECT
IIF
(
Instr(1,ShipToPlant , "#") > 0
OR Instr(1,ShipToPlant , "/") > 0
OR Instr(1,ShipToPlant , "-") > 0, ""
, ShipToPlant
)
FROM BTST
All - I wound up nesting the REPLACE() function in two separate queries. Since there's upwards of 35 non-alphanumeric characters that I needed to replace and Access limits the complexity of the query to somewhere around 20 nested functions, I merely split it into two processes. Somewhat clunky, but it worked. Should have followed the KISS principle in this case. Thanks for your help!
I know this is a really old question, but I stumbled over it whilst looking for a solution to this problem, but ended up using a different approach.
The field that I wish to update is called 'Customers'. There are 20-odd accented characters in the 'CustName' field for which I wish to remove the diacritics - so (for example) ã > a.
For each of these characters I created a new table 'recodes' with 2 fields 'char' and 'recode'. 'char' contains the character I wish to remove, and 'recode' houses the replacement.
Then for the replace I did a full outer join inside the update statement
UPDATE Customers, Recodes SET Customers.CustName = Replace([CustName],[char],[recode]);
This has the same effect as nesting all of the replace statements, and is a lot easier to manage.
This query grabs the 3 first characters and replace them with Blanks
Example: BO-1234
Output: 1234
BO: IIf(IsNumeric(Left([sMessageDetails],3)),[sMessageDetails],Replace([sMessageDetails],Left([sMessageDetails],3),""))