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?
Split functionality
Collapse
X
-
Tags: None
-
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.Originally posted by KamaleshwariPHi 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?
Take a look at this post for information about normalization.Comment
-
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.
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.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;
Good luck!Comment
Comment