neha
neha

Reputation: 267

android: How to cast values of varchar or integer type to real(float) type in sqlite

I have two columns. when i am dividing values of both table i am getting column of integer value. But I want the exact float values in the column after division. One more query i have is that when i runing this query in shell its working fine but it is giving me error when i am using it in code. Please suggest some thing.

public ArrayList<String> selectQueryForcasting(Context context,String segment,String customer, String location)
        {   
            ArrayList<String> z = new ArrayList<String>();
            Log.d("Inside Select ","Select starts for table name: Sales_Forcast");
            myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_READABLE, null);  
                            System.out.println("inside");
                                Cursor c= myDataBase.rawQuery("select ((Forecasted_Qty - Actual_Sales_Qty) * 100)/forecasted_qty from sales_forecast,customer,market_segment,org_location where market_segment.Market_Seg_Code="+segment+" and sales_forecast.Mrkt_Segment_Key=market_segment.Market_Seg_Key group by sales_forecast.time_key", null);
                System.out.println("Succcess"+c);

                if (c.moveToFirst()) {
                    do {

                        z.add(c.getString(0));
                        //Log.d("EXAMPLE","string ");

                    } while (c.moveToNext());
                }
                if (c != null && !c.isClosed()) {
                    c.close();
                }


            return z;
        } 

Upvotes: 0

Views: 3561

Answers (2)

neha
neha

Reputation: 267

I got the answer.

c= myDataBase.rawQuery("select ((cast(Forecasted_Qty - Actual_Sales_Qty as real)) * 100)/forecasted_qty,Mrkt_Segment_Key from sales_forecast,customer,market_segment,org_location  where sales_forecast.Mrkt_Segment_Key=market_segment.market_seg_key and market_segment.market_seg_code=? group by sales_forecast.time_key",new String[]{"+segment+" });

Upvotes: 0

Mike W.
Mike W.

Reputation: 1375

This should get you what you want:

select ((cast(Forecasted_Qty as REAL) - cast(Actual_Sales_Qty as REAL)) * 100)/forecasted_qty

or better yet:

select ((cast(Forecasted_Qty - Actual_Sales_Qty as REAL)) * 100)/forecasted_qty

Upvotes: 2

Related Questions