Split functionality

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KamaleshwariP
    New Member
    • Apr 2007
    • 1

    #1

    Split functionality

    Hi at work i need to split one column based on the commas.In one column there are lot of values which are separated by commas.Can u please help me to write a SQL query for teh same?
  • cnplnsk
    New Member
    • Mar 2007
    • 19

    #2
    plz explain your problem in proper way that i can get it.
    give an example...

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by KamaleshwariP
      Hi at work i need to split one column based on the commas.In one column there are lot of values which are separated by commas.Can u please help me to write a SQL query for teh same?
      This is a problem that beginner database designers find themselves confronted with; knowledgeable database designers know that you cannot effectively use data if you combine more than one piece of information into one field. The process of eliminating this from your DB layout is called Database Normalization.
      Take a look at this post for information about normalization.

      Comment

      • Michele
        New Member
        • Mar 2007
        • 4

        #4
        Hi Kamaleshwari,

        You are getting good advice about data normalization, etc.

        I run into this problem sometimes when I use other people's data and they don't always distribute it in the best data formats (and you end up stuck with what you have),

        If you are after a quick and dirty solution it may be easiest to reload the data into a new table using a LOAD DATA statement where you specify it is delimited by commas.

        You can also hack around it with some concoction of SUBSTRINGS, LEFT, RIGHT, INSTR, etc....e.g.

        Code:
        SELECT
        LEFT(field_with_commas, INSTR(field_with_commas, ",")-1) /*1 st Comma*/
        , RIGHT(field_with_commas, LENGTH(field_with_commas) -INSTR(field_with_commas, ",") )/*Minus 1st comma*/
        , LEFT(RIGHT(field_with_commas, LENGTH(field_with_commas) -INSTR(field_with_commas, ",") ), INSTR(RIGHT(field_with_commas, LENGTH(field_with_commas) -INSTR(field_with_commas, ",") ), ",")-1) /*2nd comma*/
        FROM table_with_commas;
        It may be useful to look a regular expressions, but if you just need a one off you can use basic SQL and a series of update or create tables and get there in the end.

        Good luck!

        Comment

        Working...