SQL Query to separate data into two fields

Posted by Phillip on Stack Overflow See other posts from Stack Overflow or by Phillip
Published on 2009-07-02T14:21:39Z Indexed on 2010/04/09 14:03 UTC
Read the original article Hit count: 205

Filed under:

I have data in one column that I want to separate into two columns. The data is separated by a comma if present. This field can have no data, only one set of data or two sets of data saperated by the comma. Currently I pull the data and save as a comma delimited file then use an FoxPro to load the data into a table then process the data as needed then I re-insert the data back into a different SQL table for my use. I would like to drop the FoxPro portion and have the SQL query saperate the data for me. Below is a sample of what the data looks like.

Store   Amount	Discount
1       5.95	
1       5.95	PO^-479^2
1       5.95	PO^-479^2
2       5.95	
2       5.95	PO^-479^2
2       5.95	+CA8A09^-240^4,CORDRC^-239^7
3       5.95	
3       5.95	+CA8A09^-240^4,CORDRC^-239^7
3       5.95	+CA8A09^-240^4,CORDRC^-239^7

In the data above I want to sum the data in the amount field to get a gross amount. Then pull out the specific discount amount which is located between the carat characters and sum it to get the total discount amount. Then add the two together and get the total net amount. The query I want to write will separate the discount field as needed, see store 2 line 3 for two discounts being applied, then pull out the value between carat characters.

© Stack Overflow or respective owner

Related posts about sql