Link to home
Start Free TrialLog in
Avatar of Stanton_Roux
Stanton_RouxFlag for South Africa

asked on

Split string in SQL Stored procedures

Hi There

I have a string that comes through as follows
A24+B34.56*  or sometimes comes through like this A24 or B34.78

I would like to check the string in my stored procedure.
If there is a + sign in the string I want to split the string into 2 values using the plus sign as the delimiter.
If there is no plus sign I would just like to use the value as it comes.

Thanks
Stanton

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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 KarinLoos
KarinLoos

something like... ( not tested so you might have to play with the len in the substrings ie -1 or not a bit.
SELECT
   CASE  
        WHEN CHARINDEX( '+',  myField) > 0
                  THEN    
                           SUBSTRING(  myField, 1,  CHARINDEX( '+', myField ) -1 )
        ELSE MYFIELD
  END AS LEFT_FIELD,
  CASE  
        WHEN CHARINDEX( '+',  myField ) > 0
                  THEN    
                             SUBSTRING(  myField, CHARINDEX( '+', myField ) +1 , LEN(myField) -CHARINDEX( '+', myField ) - 1   )
        ELSE
                myField
  END AS RIGHT_FIELD,
FROM myTable
oops still busy typing and angell has already given answer  Sorry