Split string contained in a column into 4 columns

SQL 2008 R2
Hy guys,
A column contains string such as A/B/C/D
I would like to split the string in four (4) columns 
Col1 Col2 Col3 Col4
  A      B      C      D
Many thanks

Try this
CREATE FUNCTION [dbo].[SplitString]
(@instring VARCHAR(MAX))
RETURNS @Splitstring TABLE(Columnslist
VARCHAR(MAX))
AS
BEGIN
DECLARE    
         @len
INT,
         @i
INT=1,
         @N
INT=1,
         @instring
VARCHAR(MAX),
         @instrings
VARCHAR(MAX)= @input +
SET @len = LEN(@instrings)
          WHILE @i<=@len
          BEGIN
            IF
SUBSTRING(@instrings,@i,1)='/'
             BEGIN
SET @instring = SUBSTRING(@instrings,@N,@i-@N)
INSERT INTO @SplitString(Columnslist)
SELECT @instring
SET @N = @i+1;
             END
             SET
@i = @i+1;
          END
RETURN ;
END
GO
Andy Tauber
Data Architect
The Vancouver Clinic
Website | LinkedIn

Similar Messages

  • Best way to split a single string containing 2 words into word1 and word2

    Whats the best way to take a string containing 2 words and split it into 2 strings ?
    eg. "red ferrari"
    string1 "red"
    string2 "ferrari"

    If your list is always going to have exactly two words, then yes.  Otherwise it depends on your requierments.

  • Split string into new column

    Hi All,
    Hoping you are able to help.
    I have a table of approx 16 items that I need to split,
    EG:
    CUSTOMER ACCEPTANCE - HAS BEEN DECLINED - DO NOT APPLY TO ACCOUNT
    CUSTOMER ACCEPTANCE -  HAS BEEN ACCEPTED - APPLY TO ACCOUNT
    CUSTOMER ACCEPTANCE - PENDING DECLINE - ESCALATION REQUIRED - RAISED IN PORTAL
    ESCALATION - RAISED - PENDING
    ESCALATION - NOT RAISED - STILL TO BE PROCESSED
    I need to Keep the first two sections, eg CUSTOMER ACCEPTANCE - HAS BEEN DECLINED in one column, and split off the remaining, eg DO NOT APPLY TO ACCOUNT into a new TEMP table to insert as a column into an existing table.
    With little SQL experience, I am having difficulties as they are all of different lengths / criteria etc. Some have 3 hyphens whilst others have 4+
    Is anyone able to help point me in the right direction with this request? I will be greatly appreciated.
    Kind Regards,
    BTMMP

    If you're trying to do this all in a SQL query or stored procedure, then you'll probably get better results on the SQL Server forums. However, if you're working with a PowerShell or VBScript that's doing the work, you're in the right place.
    Here's one example of how you could do what you're describing.  By the way, what do you want to do with the string "CUSTOMER ACCEPTANCE - PENDING DECLINE - ESCALATION REQUIRED - RAISED IN PORTAL"?  Should that be split into "CUSTOMER ACCEPTANCE
    - PENDING DECLINE" and "ESCALATION REQUIRED - RAISED IN PORTAL", or "CUSTOMER ACCEPTANCE - PENDING DECLINE - ESCALATION REQUIRED" and "RAISED IN PORTAL"?
    (In other words, should the script only split off whatever's after the final hyphen, or should it grab the first two pieces of text and split off everything else?)
    Here's an example in PowerShell which assumes that you want to separate out all text after the final hyphen in a string.  It uses a regular expression, though you could accomplish the same thing with Split, Join and Trim operations, if you prefer.
    $string = 'CUSTOMER ACCEPTANCE - HAS BEEN ACCEPTED - APPLY TO ACCOUNT'
    if ($string -match '(.*?)\s*-\s*([^-]*)$')
    $split = $matches[1], $matches[2]
    else
    $split = $string, ''
    Write-Host "Original String: $string"
    Write-Host "First Text : $($split[0])"
    Write-Host "Second Text : $($split[1])"

  • Split String value into internal table at Carriage return

    Hi All,
    I have given a string type context ( text edit  box ) to the user to enter the values. The data can have carriage returns also.
    My Requirement is that I want to split the data at carriage returns and store it in my tables.
    I tired with a constant value of '%CR_LF' as available in CL_ABAP_CHAR_UTILITIES but to no good result.
    Can the Experts suggest some solution?/??
    Thankx in advance.
    Regds,
    Srini

    Hi Srini,
    Have you looked at your string containing the text in the debugger to verify that you really have <CR>+<LF> (i.e. reflecting CL_ABAP_CHAR_UTILITIES=>CR_LF, which should be easily visible when looking at the hex codes of the string)? Maybe it's just a <LF> (i.e. CL_ABAP_CHAR_UTILITIES=>NEWLINE). Might be good to do a REPLACE first using a regular expression for identifying the line feeds and then doing the SPLIT.
    Cheers, harald
    p.s.: If you still cannot get it to work, provide some more details of what you're doing...

  • Split a border container or a group into 2

    Hello,
    I have a container (a border container or a group), now depending on a specific point along the width of the container i want to divide the container into two individual containers.
    Is there any way to do it using flex and actionscript ?
    please help!

    Hello Onkari,
    Thank for the reply.
    I guess i was not clear in explaning what i wanted.
    I have drawn a waveform of a music track and added it into a border container.
    I actually want to split the waveform into two from a point, so i need to split the container.
    so is there any to do so?

  • Split String at delimiter into table.

    Hi Experts,
    I have a requirement wherein i need to split aaaa#bbbbbbbbbbbb#ccc#dddddddddddddddddddddd#e# into internal table as below:
    Header 1
    Header 2
    1
    aaaa
    2
    bbbbbbbbbbbb
    3
    ccc
    4
    dddddddddddddddddddddd
    5
    e
    how can this be done.
    Thanks & Regards,
    Anil

    Hi Ashok,
    split <string> at '#' into table it.  -Doesn't work
    split <string> at SPACE into table it. - Works
    But same statement with '#' is not working.
    Feeling strange.
    Thanks,
    Anil

  • Writing strings containing \0 characters into a TextField

    Hi,
    I have a string containing one or more '\0' characters. I need to write it into a TextField and, for the purpose, I use the setText method.
    What I get then is a substring containing only the characters before the first \0.
    If I use a JTextField then I am able to see all the characters but if I try to copy and paste the content of the JTextField (using CTRL-C and CTRL-V) I get again a substring terminated by the first \0.
    Could someone help me?

    Hello again,
    I wrote this program that will copy from one JTextField to another. are you trying to past to another application, if I past the string into ultraedit (text editor) I get the first word, I can't see a way around that?
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class SimpleApp extends JFrame {
    public static void main(String args[]) {
    SimpleApp aFrame = new SimpleApp();
    JTextField myTextField = new JTextField("one\0two\0three\0four\0");
    JTextField myTextField2 = new JTextField();
    public SimpleApp() {
    super("Frame");
    getContentPane().setLayout(new BorderLayout());
    setResizable(false);
    myTextField.setColumns(30);
    myTextField2.setColumns(30);
    getContentPane().add(myTextField, "Center");
    getContentPane().add(myTextField2, "South");
    pack();
    show();
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) { System.exit(0); }
    }

  • Split String into two

    HI,
    How to Split String into two parts at delimiter

    HI,
    REPORT ZSTRING.
    DATA: LENGTH TYPE I,    
          REMAINING_LENGTH TYPE I ,   
          NEXT_POINTER TYPE I    ,   
          FIRST_HALF(20)  TYPE C ,    
          SECOND_HALF(20) TYPE C ,    
          TEMP TYPE I .
    PARAMETER: WORD(35) TYPE C . "INPUT WORD
    START-OF-SELECTION.
    LENGTH = STRLEN( WORD ).      "Length of the input String
    SEARCH WORD FOR '/'.
        IF SY-SUBRC = 0 .  
          IF SY-FDPOS > 0.   
          MOVE WORD+0(SY-FDPOS) TO FIRST_HALF.  
          ENDIF.  
       TEMP = SY-FDPOS + 1. 
       IF TEMP <> LENGTH.    
       NEXT_POINTER = SY-FDPOS + 1.    
       REMAINING_LENGTH = ( LENGTH - SY-FDPOS ) - 1.    
       MOVE WORD+NEXT_POINTER(REMAINING_LENGTH) TO SECOND_HALF.  
       ENDIF.
    ENDIF.
    WRITE:/'Input String:', WORD .
    WRITE:/'First  Half:', FIRST_HALF.
    WRITE:/'Second Half:', SECOND_HALF.
    *-- End of Program
    Reward Points if found helpfull..
    Cheers,
    Chandra Sekhar.

  • Split string into two based on end of word and max length

    I have the following procedure, which is passed a comma separated string as it input, and it has to split it into two, and the split has to occur at the end of a word, and the first split part can't be more than 15 in length.
    Is there an more efficient way of achieving this? I have a loop in my main code that calls this procedure and in effect a nested loop has been created here.
    e.g. Hello, everyone, welcome, to, split, string' would be split into 'Hello' and 'everyone, welcome, to, split, string'
    create or replace procedure split_str
                   (pi_str  in  varchar2,
                    po_str1 out nocopy varchar2,
                    po_str2 out nocopy varchar2) is
      i number;
    begin
      if (len(pi_str) <= 15) then
        po_str1 := pi_str;
        po_str2 := NULL;
      else
        i := 1;
        loop
          exit when len(SUBSTR(pi_str, 1 ,INSTR(pi_str, ',', 1, i)-1)) > 15;
          po_str1 := SUBSTR(pi_str, 1 ,INSTR(pi_str, ',', 1, i)-1);
          po_str2 := SUBSTR(pi_str, INSTR(pi_str, ',', 1, i)+2);
          i := i+1;
        end loop;
      end if;
    end split_str; 

    You want to find the last space character before the 15th character.
    x := INSTR ( SUBSTR ( pi_str, 1, 15) , ' ' , -1)Then the first string is SUBSTR(pi_str,1,x) and the second string is SUBSTR(pi_str,x+1)

  • How to split this string(char1)char2(char3)char4 into (char1)char2 , .. etc

    how to split this string (char1)char2(char3)char4 into (char1)char2 , (char3)char4?
    String[] result = "(char1)char2(char3)char4".split("\\(");I want :
    result[0] = "(char1)char2" and
    result[0] = "(char3)char4"
    acutally char1,char2,char3, char4 ... is in the form of the below.
    (any charactors except round brace)any charactors except round brace(any charactors except round brace)any charactors except round brace
    I prefer String.split and Pattern.compile().split.
    Edited by: iamjhkang on Feb 5, 2009 3:37 PM
    Edited by: iamjhkang on Feb 5, 2009 3:41 PM

    iamjhkang wrote:
    especially on
    ?= and ?<
    Thanks.The following:
    (?=...)   // positive look ahead
    (?!...)   // negative look ahead
    (?<=...)  // positive look behind
    (?<!...)  // negative look behindare all "look-arounds". See: [http://www.regular-expressions.info/lookaround.html]

  • How to Split string ("39934,,\"VIE\",\"Cong, hoa, xa, hoi\"") ???

    My string is:
    String string = "39934,,\"VIE\",\"Cong, hoa, xa, hoi\""
    Now, i want to split into array:
    a[0]="39934"
    a[1]=""
    a[2]="\"VIE\""
    a[3]="\"Cong, hoa, xa, hoi\""
    How i do?

    thanhhaunv wrote:
    Now, i want to split into array:
    a[0]="39934"
    a[1]=""
    a[2]="\"VIE\""
    a[3]="\"Cong, hoa, xa, hoi\""[String.split()|http://java.sun.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29] will do the basics for you; but it's naive.
    ie, If any of your quoted strings contain commas and you don't want them split, you'll have to put them back together in a step 2.
    Winston

  • String.class without split(String regex)

    hello,
    This is my problem:
    I am using eclipse with j2sdk1.4.2 which
    implement
    the clase String.class with
    method replaceAll(String regex, String replacement) and
    method split(String regex)
    PAth: home_j2sdk1.4.2/jre/lib/rt.jar 25.762kb
    In this way:
    String columns[] = line.split("\t");
    for (int i=0; i<columns.length; i++)
    ws.addCell(new Label(i,row,columns.replaceAll("\"","")));
    I move the code above to websphere Application Developer 5.1.2
    which use Home_Websphere/eclipse/jre/lib/rt.jar 8.827 kb
    this jar in yours String.class not contains the
    method replaceAll(String regex, String replacement) and
    method split(String regex)
    I need to find the use my code above with that methods,
    what i need to do ???
    possible,i need to find the source to String.java of j2sdk1.4.2
    which implement the methods, but where ???
    or there is another way to do it ???
    Any help is greatly appreciated.

    If websphere is supposedly 1.4 compatible, arethey
    "allowed" to leave out methods from the JFC? Not sure what the JFC is, but no, they would not be
    allowed to leave out any methods.
    JFC - Java Foundation Classes. I think they started calling what might be considered the Java Core the JFC when they first added Swing to the distribution. Maybe the term is no longer used.
    RRD-R      
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • To check that string contains space

    Hi...
    I have one char string, If that string contains any space then I want to display a message.
    how to check that string contains 'space'.
    Regards,
    Rohit

    Hi, you can do a small work.
    you can split the char string into substrings at spaces.
    if there is space then it will be splitted and then you can check the sy-subrc.
    if sy-subrc = 0,
    then the char string had some space in it.
    regards
    jayati

  • How to parse a string containing xml data

    Hi,
    Is it possible to parse a string containing xml data into a array list?
    my string contains xml data as <blood_group>
         <choice id ='1' value='A +ve'/>
         <choice id ='2' value='B +ve'/>
             <choice id ='3' value='O +ve'/>
    </blood_group>how can i get "value" into array list?

    There are lot of Java XML parsing API's available, e.g. JAXP, DOM4J, JXPath, etc.
    Of course you can also write it yourself. Look which methods the String API offers you, e.g. substring and *indexOf.                                                                                                                                                                                                                                                                                                                                                                                                               

  • Split String to use in SQL

    How do I split the string Bill Whatever Whatever Clinton into firstname "Bill" middlename "Whatever Whatever" and lastname "Clinton".
    BR Badleif

    StringTokenizer st=new StringTokenizer(strFName," ");
    String [] strArr = new String[3];
    for(int i=0;i<3;i++)
    strArr[i] = st.nextToken();
    }return strArr;
    String strFName=strArr[0];
    String strMNAme=strArr[1];
    etc.........

Maybe you are looking for

  • Blank screen after a change in the motherboard's Bios

    Hi, Recently, I have bought a MSI motherboard - 865PE Neo2 FIS2R. The computer works fine after I installed all the components and applications (including Windows XP [included Service Pack 1], Intel Chipset Software Installation Utility and all the d

  • Install of oracle 8.1.6 on redhat linux

    i installed oracle 8.1.6 on redhat linux 7.0 after installation while creating a default database it is taking lot of time, i tried to create one more database using database assistant it is also taking lot of time initialize i don't know how to set

  • Have PowerbookG4 bought in 2002.  No $ to buy new, but should I?

    I have a Powerbook G4, purchased in June 2004 and running 10.3.9. I was planning to buy a new one next summer but this one has issues. I can't use any USB 2.0 divices; when I tried to use the same external back up drive this week that I have been usi

  • How to play analog sound with PC input

    Hello, I have connected a pc to the pc input (VGA sub D15) in my TV 46TL933. I am looking to connect the analog audio output from the pc into the TV. I already tried to connect the audio signal to the component input, but the tv doesn't listen that i

  • How to create manually the HTML-DB?

    Hello, I had create a database with CREATE DATABASE. After that I had execute the following scripts: @D:\OracleXE\app\oracle\product\10.2.0\server\rdbms\admin\catalog.sql; @D:\OracleXE\app\oracle\product\10.2.0\server\rdbms\admin\catblock.sql; @D:\Or