Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how can i sub string from field for example i have this in
my field "Hyper super market - Saudi Arabia"
i need all words before "-" this what i need "Hyper super market"
how can i remove all char after "-"

What I have tried:

i'v try also this and not working will

C#
=mid(Fields!Description.Value,0,instr(Fields!Description.Value,"-")+1)
Posted
Updated 17-Feb-20 22:28pm

1 solution

Have you tried to use Left function, instead of Mid?
=Left(Fields!Description.Value, InStr(Fields!Description.Value, "-")-1)

More at: Expression Examples (Report Builder) - SQL Server Reporting Services (SSRS) | Microsoft Docs[^]

[EDIT]
Richard Deeming wrote (in the comment):
The IIF function evaluates both branches in all cases, which is why you're getting an error.

You'll need to use IIF in a slightly different position:

=Left(Fields!Description.Value, IIF(InStr(Fields!Description.Value, "-") > 0, InStr(Fields!Description.Value, "-") - 1, Len(Fields!Description.Value)))

[/EDIT]
 
Share this answer
 
v4
Comments
mohammed mqi 18-Feb-20 4:32am    
it's give me #Error
Maciej Los 18-Feb-20 5:43am    
Sorry, my bad. Updated (removed 1 from Instr function).
mohammed mqi 18-Feb-20 8:26am    
thanks it works fine but i have an issue if there's no dash"-" in my word it's give me error how can i avoid that?
also i try this and and also show me error for word doesn't contain dash

=IIf(InStr(Fields!Description.Value, "-") > 0,Left(Fields!Description.Value, InStr(Fields!Description.Value, "-")-1),Fields!Description.Value)
Maciej Los 18-Feb-20 8:57am    
Seems, you have to inspect your data. Sorry, but i have not an access to them.
Richard Deeming 18-Feb-20 15:06pm    
The IIF function evaluates both branches in all cases, which is why you're getting an error.

You'll need to use IIF in a slightly different position:
=Left(Fields!Description.Value, IIF(InStr(Fields!Description.Value, "-") > 0, InStr(Fields!Description.Value, "-") - 1, Len(Fields!Description.Value)))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900