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


SSRS 2008 substring process


SSRS 2008 substring process

Author
Message
wendy elizabeth
wendy elizabeth
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1247 Visits: 775
In an SSRS 2008 existing report, I have one column that I need to separate the values. I am not allowed to change the database. I am told that I need to work with the data as it is.

The values in the column look like the following

099 11-12 Midwest Plumbers.

I need to split the data to look like

1. 099 is the Customer Number.

2. 11-12 is the year the customer data was valid.

3. Midwest Plumbers is the name of the company.

Notes:

1. The delimiter between the 3 fields is " " (one space).

2. The company name can contain lots of spaces.

3. There are 3 fields that need to be separated out which are:

a. Customer Number,

b. Effective Years,

c. Customer Name.

I 'best' solution, I have so far is:

=Mid(Parameters!pCust.Value,InStr(Parameters!pCust.Value," ") + 1,Len(Parameters!pCust.Value)-Instr(Parameters!pCust.Value," ")).

However the above only gets me the first value.

Thus can you show me how to split up the in this column within SSRS?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87662 Visits: 41121
wendy elizabeth (11/1/2013)
In an SSRS 2008 existing report, I have one column that I need to separate the values. I am not allowed to change the database. I am told that I need to work with the data as it is.


I don't know the syntax of SSRS but here's what works in T-SQL. You would need to replace SUBSTRING with MID and CHARINDEX with the equivalent (INSTR?) SSRS function. CHARINDEX finds the position of the first expression in the second expression starting at the optional third express)

Here's a working example. Of course, you could change @SomeString to the column name you want to parse and add a FROM clause for the table that column exists in but I wanted to keep this simple. They COULD add persisted computed columns to the table to permanently split the data without any changes to a well written front-end.

In the following, the only assumption that I made about the data is that the CustomerNumber column will ALWAYS be 3 characters as indicated by the leading zero's. Heh... having such formatted customer numbers is yet another mistake the people that designed this made. This is a really strange requirement because they want normalized data to appear in the report but refuse to normalize the data in the database where it should be.


DECLARE @SomeString VARCHAR(100);
SELECT @SomeString = '099 11-12 Midwest Plumbers';

SELECT CompanyNumber = SUBSTRING(@SomeString,1,3)
,ValidYear = SUBSTRING(@SomeString,5,CHARINDEX(' ',@Somestring,CHARINDEX(' ',@SomeString,5))-5)
,CompanyName = SUBSTRING(@SomeString,CHARINDEX(' ',@SomeString,5)+1,8000)
;



Here are the results...

CompanyNumber ValidYear CompanyName
------------- --------- ----------------
099 11-12 Midwest Plumbers



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
kiril.lazarov.77
kiril.lazarov.77
SSC-Addicted
SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)

Group: General Forum Members
Points: 466 Visits: 2248
wendy elizabeth (11/1/2013)
In an SSRS 2008 existing report, I have one column that I need to separate the values. I am not allowed to change the database. I am told that I need to work with the data as it is.

The values in the column look like the following

099 11-12 Midwest Plumbers.

I need to split the data to look like

1. 099 is the Customer Number.

2. 11-12 is the year the customer data was valid.

3. Midwest Plumbers is the name of the company.

Notes:

1. The delimiter between the 3 fields is " " (one space).

2. The company name can contain lots of spaces.

3. There are 3 fields that need to be separated out which are:

a. Customer Number,

b. Effective Years,

c. Customer Name.

I 'best' solution, I have so far is:

=Mid(Parameters!pCust.Value,InStr(Parameters!pCust.Value," ") + 1,Len(Parameters!pCust.Value)-Instr(Parameters!pCust.Value," ")).

However the above only gets me the first value.

Thus can you show me how to split up the in this column within SSRS?


Hi,

Try the split function

a. Customer Number,
=(Split(Parameters!pCust.Value," ")).GetValue(0)

b. Effective Years,
=(Split(Parameters!pCust.Value," ")).GetValue(1)

c. Customer Name.
=Trim(Replace(Replace(Parameters!pCust.Value,(Split(Parameters!pCust.Value," ")).GetValue(0)," "), (Split(Parameters!pCust.Value," ")).GetValue(1), " " ))

edit: changed fields to parameters
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