Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SSRS 2008 substring process Expand / Collapse
Author
Message
Posted Friday, November 1, 2013 1:12 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 8:17 AM
Points: 362, Visits: 422
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?
Post #1510780
Posted Sunday, November 3, 2013 7:37 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:44 PM
Points: 36,763, Visits: 31,218
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1510890
Posted Monday, November 4, 2013 8:05 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 4:59 PM
Points: 275, Visits: 1,665
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
Post #1511127
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse