Link to home
Start Free TrialLog in
Avatar of bbraman
bbraman

asked on

double split - splitting a string by more than one delimiter

Each line of data below is stored in a single nvarchar field.

CUSTOM,FedEx by Monday Afternoon (11th),18,0
CUSTOM,Standard US Mail,6.5,0
CUSTOM,US Priority Mail + Delivery Conf.,7.25,0
CUSTOM,FedEx by Thursday Morning (21st),35,0
CUSTOM,FedEx by Wednesday Morning (6th),35,0
CUSTOM,FedEx by Friday Afternoon (8th),18,0
CUSTOM,FedEx by Thursday Afternoon (7th),18,0
CUSTOM,FedEx by Friday Afternoon (3rd),18,0
CUSTOM,FedEx by Wednesday Afternoon (6th),30,0
USPS,USPS Airmail Letter Post ,7.55,0
CUSTOM,UPS Ground,12,0

I am creating a view in SQL Server 2000 where I need the data above displayed as below.

FedEx by Monday Afternoon
Standard US Mail
US Priority Mail + Delivery Conf.
FedEx by Thursday Morning
FedEx by Wednesday Morning
FedEx by Friday Afternoon
FedEx by Thursday Afternoon
FedEx by Friday Afternoon
FedEx by Wednesday Afternoon
USPS Airmail Letter Post
UPS Ground

Please Note that data after '(' is always discarded.

Please let me know what syntax will achive my goal.  

Thank You.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Try something like this (although it might be better accomplished in a UDF):

Select      SUBSTRING(@YourColumnName, CHARINDEX(',', @YourColumnName) + 1, CHARINDEX('(', @YourColumnName) - CHARINDEX(',', @YourColumnName) - 1)
Here is the complete syntax:
Select      SUBSTRING(YourColumnName, CHARINDEX(',', YourColumnName) + 1, CHARINDEX('(', YourColumnName) - CHARINDEX(',', YourColumnName) - 1)
From      YourTableName
Avatar of bbraman
bbraman

ASKER

Thank you for your attention to my question.

The complete syntax you provided returned an 'invalid length parameter passed to the substring function" message.
That is because not all of the columns have "(".  You have a couple of options:
1. Enhance that query to allow for the possibility of no "(" or
2. Create a function (UDF) to accomplish the same.  The advantage here is that it is easier to maintain.

Let me know what is your poison.

Here is the UDF solution:

Create Function udf_GetValue(@Value varchar(100))

Returns varchar(100)

As

Begin

Declare @Pos tinyint
Set @Pos = CHARINDEX(',', @Value)

If @Pos > 0
   Begin
      Set @Value = SUBSTRING(@Value, @Pos + 1, 100)
      Set @Pos = CHARINDEX('(', @Value)
      If @Pos > 0
         Begin
            Set @Value = LEFT(@Value, @Pos - 1)
         End
   End

Return @Value

End

You would then call it as follows:

Select     dbo.udf_GetValue(YourColumnName)
From     YourTableName
Avatar of bbraman

ASKER

The UDF approach is fine, but the function does not have the right results:

FedEx by Monday Afternoon
Standard US Mail,6.5,0
US Priority Mail + Delivery Conf.,7.25,0
FedEx by Thursday Morning
FedEx by Wednesday Morning
FedEx by Friday Afternoon
FedEx by Thursday Afternoon
FedEx by Friday Afternoon
FedEx by Wednesday Afternoon
USPS Airmail Letter Post ,7.55,0
UPS Ground,12,0

Still do not match what I am looking for, see below:

FedEx by Monday Afternoon
Standard US Mail
US Priority Mail + Delivery Conf.
FedEx by Thursday Morning
FedEx by Wednesday Morning
FedEx by Friday Afternoon
FedEx by Thursday Afternoon
FedEx by Friday Afternoon
FedEx by Wednesday Afternoon
USPS Airmail Letter Post
UPS Ground

Thank you for your attention to my question.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbraman

ASKER

That did it, Thank You.