SSRS Reporting Expression to Split Full Name Field - reporting-services

I've been trying to get this to behave with no luck.
The database base field only has FULL NAME field, which includes Middle initials on some cases and no middle initials in some other cases.
I've been trying to display LastName, FirstName by using this ssrs expression below: While this works in names that doesn't have middle initials or name on the field.... for those names that includes middle initials/name, we run into issues with displaying.
=Right(Fields!agent_name.Value,Len(Fields!agent_name.Value)-Instr(Fields!agent_name.Value," ")) & ", " & (Left(Fields!agent_name.Value,Instr(Fields!agent_name.Value," ")))
But it includes the Middle Initials to display first.. As an example:
If the fullname field is John S Doe, above expression displays as:
S Doe, John
What I need to display is:
Doe, John
How do I set my expression to get rid of the Middle initial/name to display?
I've done a great amount of research and tried many diff expressions but no luck.. thanks in advance.

While slightly inefficient, this is a clean way to accomplish the result:
=Split(Fields!agent_name.Value," ")(Split(Fields!agent_name.Value," ").Length-1) + ", " + Split(Fields!agent_name.Value," ")(0)
Edit: The above indeed does not work with suffixes. The expression below will remove a middle initial if its the second name and has a length of 1, which you could use in a calculated field. But it would fail for first name "Ann Marie" and the first/last swap would need to be handled separately.
=IIF(Split(Fields!agent_name.Value," ")(1).Length=1,Replace(Fields!agent_name.Value, " " + Split(Fields!agent_name.Value," ")(1) + " "," "),Fields!agent_name.Value)
The short of it is that you really would be best served handling this in code. But even so, if your fullname field is space-inclusive beyond what you want to seperate on there arises the potential for ambiguous cases, like the suffix- and initial-less "Ann Marie van Der Beek".

Try using this expression:
=RIGHT(
Fields!agent_name.Value,LEN(Fields!agent_name.Value)-InStrRev(Fields!agent_name.Value," "))
& ", " & LEFT(
Fields!agent_name.Value,
InStr(Fields!agent_name.Value," ")-1
)
It is a native SSRS solution however I recommend using custom code to get an maintainable solution.
UPDATE: Regex solution:
=System.Text.RegularExpressions.Regex.Replace(
Fields!agent_name.Value,
"(\w+)\s+(\w+\.*\s)?(\w+)",
"$3, $1"
)
Let me know if this helps.

Related

MS Access Calculated Field - Creating dynamic expression

I need to be able to create an ID CODE from the plant name to be in the format of AAAA_AAAAAAAAA_A00000 (A for alpha). Some thing like this;
Acacia dealbata
ACAC_DEALBATA_T00001
I can do the first section using Left([FieldName],4) but the middle section I'm having trouble with as it will differ depending on the remaining number of words in the name.
Acer palmatum Atropurpureum would become ACER_PALMAATRO_T00002
Acer palmatum Dissectum Rubrum would become ACER_PALDISRUB_T00003
My attempt at the middle section is;
IIf(Len([FieldName]) - Len(Replace([FieldName], " ", "")) + 1 = 2, Mid([FieldName], InStr(1, [FieldName], " "), (9 / (Len([FieldName]) - Len(Replace([FieldName], " ", ""))))), "")
And returns the following Error Message.
The end section depends on the SUPPLIER of the image of the plant and is given a specific prefix; L, H, or R. Followed by the records autonumber in the table. This prefix is stored in another table I have called SUPPLIER.
I do not know how to collect values from fields of other tables into another so any advice on this is appreciated.
If anyone is able to help please.

DSUM - Data Typed Mismatched in Criteria Expression

Withdrawal: DSum("[quantity]","Withdrawal","[part_number]= '&[part_number]&'")
I am getting the above error due to the statement above.
I have an Orders table, would like to collate and display the withdrawals quantity for each part number. Data type is number, am I missing anything here?
Your help is much appreciated.
You're working with 2 types of quotes, and doing it improperly. The proper code should be:
Withdrawal: DSum("[quantity]","Withdrawal","[part_number]= " & [part_number])
In the code above, the second [part_number] was still inside a string so not filling properly. As a result you were comparing part numbers with the string [part_number]
Missing a quote after the first apostrophe and before the second.
Withdrawal: DSum("[quantity]","Withdrawal","[part_number]= '" & [part_number] & "'")
part_number is a text field? If not, remove the apostrophes.

Concatenate name fields in an Access query using IIF() without printing spaces?

First of all, I am a newbie. Please keep that in mind...
That said, I have used a simple formula to concatenate name fields in a student database in Access using a query (I am using Access 2013 but must maintain compatibility with Access 2010). It reads as such:
Student Name: IIf(IsNull([Preferred First Name]),[First Name],[Preferred First Name]) & " " & [Last Name]
Which shows the student's preferred first name and last name, or first name and last name. If it turns out first (preferred or given) is null and/or last is null, obviously it prints null where appropriate.
Example of formula output using fictional names
Well, that is where the issue comes in. As you can see in the example, one student only has first name Christine, which prints as "Christine ". Another only has last name Alexander, which prints as " Alexander". It is printing the space no matter what.
Now I now it is possible using Iif to get around this -- it's used in a sample database like so:
Student Name: IIf(IsNull([Last Name]),IIf(IsNull([First Name]),[First Name]),IIf(IsNull([First Name]),[Last Name],[First Name] & " " & [Last Name]))
I want to incorporate the "Preferred First Name" field into that formula, but I just can't wrap my head around it. It seems to work backwards from how I would say this in natural speech, and it seems to be missing values. In plain English I envision it like this:
If [Last Name] is null, print only [Preferred First Name], unless that is null, in which case print only [First Name], unless that is also null, in which case print null.
Otherwise if [Last Name] has a value, print [Preferred First Name] & " " & [Last Name], unless [Preferred First Name] is null, in which case print [First Name] & " " & [Last Name], unless [First Name] is also null, in which case just print [Last Name].
(To clarify, there are sometimes cases where we are unable to get a student's full name, so I can't simply make all fields required, which is why I want to make sure it prints correctly regardless of what data is available.)
I apologize if this is too simple a question for this site, but it seemed like the best place to ask. Thank you for any help.
You want to use the Trim function. So, your string would look like:
Student Name: Trim(IIf(IsNull([Preferred First Name]),Trim([First Name]),
Trim([Preferred First Name])) & " " & Trim([Last Name]))
Trim each individual field (just in case the data got entered with an extra space or 2, which happens occasionally), and then trim the entire resultant string to get rid of any spaces as well.

How to align each string in text in field column of ssrs

I have a field like this in the column field in ssrs.
QUINIDINEssssssssssssssssssssssssssssssssssssssssssssssssssssss GL INJ 80MG/ML
FLECAINIDEssssssssssssssssssssssssssssssssss TAB 50MG
TAMBOCORsssssssssssssssssssssssssssssssssssssssssssss TAB 50MG
and how can i align align like this...
QUINIDINEssssssssssssssssssssssssssssssssssssssssssssssssssssss GL INJ 80MG/ML
FLECAINIDEssssssssssssssssssssssssssssssssss(emptyspaceeeeeeeee)TAB 50MG
TAMBOCORsssssssssssssssssssssssssssssssssssssssssssss(emptyspa)TAB 50MG
each part of string align properly...
Any help...
Well, there are two ways to do this that I worked out, and neither is particularly elegant so I'd be surprised if someone doesn't have a better solution.
"t.a" in the code below is your table and column.
The SQL way:
SUBSTRING(t.a,1,CHARINDEX(' ',t.a,1))+REPLICATE('_',20)+REVERSE(SUBSTRING(REVERSE(t.a),1,CHARINDEX(' ',REVERSE(t.a),1)))
Change the value being replicated above from '' to ' ' and you are in business. I left it as '' to illustrate what it's doing.
The SSRS way, which is better from a DB standpoint:
=Mid(Fields!a.Value,1,InStr(Fields!a.Value," "))+StrDup(20,"_")+StrReverse(Mid(StrReverse(Fields!a.Value),1,InStr(StrReverse(Fields!a.Value)," ")))
This is precisely the same formula, executed by SSRS instead of by SQL Server.
I once used Switch to append spaces to a column field, like in the following example:
=Switch(LEN(Fields!FamilyMemberName.Value) = 1, " ",
LEN(Fields!FamilyMemberName.Value) = 2, " ",
LEN(Fields!FamilyMemberName.Value) = 3, " ")
But I didn't have to bother about the expression getting longer, since I know exactly how many spaces I should append and it wasn't more than 15.
You could use Switch in your case too or you could write a VB function in the Code tab of Report Properties window and pass the values, use some loops to generate the spaces you require.
Or it's much easier in MS SQL, all you got to do is append SPACE(NoOfSpacesInInteger) to the field.

Access / DCount gives a TExt field and not INT field?

In Access 2010, I have two tables 'Contact' and 'PhoneCalls'.
I created this Query for 'Contact' as I want to see how many times I called a contact.
Query:
SELECT Contact.*, DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID]) AS Call
FROM Contact
I have build this query using the following expression:
Call: DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])
It works fine except that it creates a TEXT field instead of a NUMBER field. For instance, I need to sort this query out, but I can only sort it "A to Z" and not "smallest to largest" as it should be.
Do you have any idea on how I can solve that?
You could use CInt() to force the call count to be an integer:
SELECT Contact.*, CInt(DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])) AS Call
FROM Contact;
Note also that using DCount() in this way is rather inefficient. If that approach works to your satisfaction then continue to use it for now, but don't be surprised if it starts to bog down as the tables grow.