Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have table(tbl_product_price) with three columns (product_id,product_price,quantity)

The table value will be like this,
SQL
product_id     product_price     quantity
   1             500              1Kg
   2             250              500gm
   3             100              12pcs
   4             150              12nos

My sql query is:
SQL
select quantity from tbl_product_price where product_id=2

i want output quantity as integer,i took it up with a dataset in C#,
now i want to split quantity column into integer and character.

For ex, if i select product_id=2, 500gm will split into,
C#
int quantity1=500
string str1=gm

if i select product_id=3, 12pcs will split into,
C#
int quantity1=12
string str1=pcs

How to do like this in C#?
I tried google, but not yet got perfect solution.

Thanks in advance,
Rajeshwaran T
Posted
Comments
Richard Deeming 28-Oct-15 10:55am    
Seems like a bad design to me. It would be better to have two columns: an integer for the quantity, and a quantity unit. The quantity unit should be a lookup to a separate table with a list of valid units, possibly using a surrogate key to save space on the pricing table.
Rajesh waran 28-Oct-15 10:58am    
Yes I too have same opinion. But there are thousands of record so I can't change the design now.

A regular expression seems to be what you need here:
C#
using System.Text.RegularExpressions;

Regex regex = new Regex(@"^(?<Quantity>[0-9]+)(?<Unit>[^0-9]+)$", RegexOptions.Compiled | RegexOptions.CultureInvariant);

string input = "12pcs";
Match m = regex.Match(input);
int quantity = int.Parse(m.Groups["Quantity"].Value);
string unit = m.Groups["Unit"].Value;

// quantity = 12
// unit = "pcs"

Hope this helps.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Oct-15 10:02am    
5ed.
—SA
phil.o 28-Oct-15 10:22am    
Thanks Sergey :)
Rajesh waran 28-Oct-15 10:40am    
Thank u I'll try this and come back to u.
Another way is to use Linq[^] + Regex.Replace method[^].

Imagine, you did load the data into DataTable object, so you're able to split quantity data into quantity(string) and unit(int):
C#
DataTable dt  = new DataTable();
dt.Columns.Add(new DataColumn("id", typeof(int)));
dt.Columns.Add(new DataColumn("product_price", typeof(int)));
dt.Columns.Add(new DataColumn("quantity", typeof(string)));
dt.Rows.Add(new Object[]{1, 500, "1Kg"});
dt.Rows.Add(new Object[]{2, 250, "500gm"});
dt.Rows.Add(new Object[]{3, 100, "12pcs"});
dt.Rows.Add(new Object[]{4, 150, "12nos"});

string patternq = @"\d+";
string patternu = @"\B[A-Z]+";

var result = dt.AsEnumerable()
    .Select(x=>new
    {
        id = x.Field<int>("id"),
        price = x.Field<int>("product_price"),
        quantity = Convert.ToInt32(Regex.Replace(x.Field<string>("quantity"), patternu, string.Empty ,System.Text.RegularExpressions.RegexOptions.IgnoreCase)),
        unit = Regex.Replace(x.Field<string>("quantity"), patternq, string.Empty, System.Text.RegularExpressions.RegexOptions.None)
    }).ToList();

foreach(var obj in result)
{
    Console.WriteLine("{0}\t{1}\t{2}\t{3}", obj.id, obj.price, obj.quantity, obj.unit);
}
 
Share this answer
 
Comments
Rajesh waran 29-Oct-15 3:46am    
Thank you Maciej.
Maciej Los 29-Oct-15 3:47am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900