SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SSRS expressions to get part of a string


SSRS expressions to get part of a string

Author
Message
rozanjos
rozanjos
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 30
Hi,

I need to write expression in my report to get the characters between "_" and "-".

I have two columns in my report, group_number and group_name. Using the characters in the group_name, I need to write the group_number.

For example,

Suppose the group_name is:

abc_def-ghi

Then my group_number should be "def".

And the number of characters can vary from 2 to 3. That is, group number can be "de" or "def". The thing is it should be between underscore(_) and (-).

Thanks in advance.
Gazareth
Gazareth
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7690 Visits: 6045
You can do it in SQL in the dataset, or an expression in the report itself, both using substring and charindex/indexof:

SQL:
SUBSTRING(group_name, CHARINDEX('_', group_name), CHARINDEX('-', group_name) - CHARINDEX('_', group_name))

Expression:
=Substring(Fields!group_name.Value, Fields!group_name.IndexOf('_'), Fields!group_name.IndexOf('-') - Fields!group_name.IndexOf('_'))

Note both are untested & you might need to put in something to handle cases where _ or - don't appear.

Cheers
Gaz
rozanjos
rozanjos
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 30
Hi Gaz,

Thanks for the response.

I tried the below expression and it worked.

=Mid(Fields!group_name.Value, InStr(Fields!group_name.Value,"_")+1, (InStr(Fields!group_name.Value, "-") - InStr(Fields!group_name.Value, "_") - 1))
Gazareth
Gazareth
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7690 Visits: 6045
No problem, glad you found a solution!
thehose_2000
thehose_2000
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 81
Well I'm trying the same thing here and I get a "#error" in the field using the following expression, what am I doing wrong...:

=Mid(Fields!storageDimensionCode.Value,InStr(Fields!storageDimensionCode.Value," "),(InStr(Fields!storageDimensionCode.Value," ") - InStr(Fields!storageDimensionCode.Value,"H")))

Thank you all for posting.... Cool
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search