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.
Microsoft SQL Server

Avatar of undefined
Last Comment
bbraman

8/22/2022 - Mon
Anthony Perkins

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

Select      SUBSTRING(@YourColumnName, CHARINDEX(',', @YourColumnName) + 1, CHARINDEX('(', @YourColumnName) - CHARINDEX(',', @YourColumnName) - 1)
Anthony Perkins

Here is the complete syntax:
Select      SUBSTRING(YourColumnName, CHARINDEX(',', YourColumnName) + 1, CHARINDEX('(', YourColumnName) - CHARINDEX(',', YourColumnName) - 1)
From      YourTableName
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Anthony Perkins

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.

Anthony Perkins

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
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Anthony Perkins

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
bbraman

ASKER
That did it, Thank You.