tags:

views:

68

answers:

2

There are three columns of cost, participant, single_cost and wants to write SQL to classify the value that divided cost by participant into single_cost. I work to leave it off when decimal fraction appeared, and I can enter, but I say, and would knowing a method teach a value on this occasion? Each column is int type. Thanking you in advance.

ex At the time of cost = 15000, participant = 7, I become single_cost = 2,142.8, but register myself with a database for 2143.

+1  A: 

If I understand correctly you want the floating point value to be kept in your database. For that to happen you need to cast your division result to float in the following way: CAST(X/Y AS FLOAT), where X and Y and your field names.

Please note the data type of single_cost must be also FLOAT.

Depending on the database you have, another way to achieve this is to multiply either the numerator or denominator by "1.0".

For example:

UPDATE Table
SET single_cost = (1.0 * cost) / participant

The previous solution would be:

UPDATE Table
SET single_cost = CAST(cost AS FLOAT) / participant

Hope that helps.

Roee Adler
A: 

You could do something like this (assuming t-sql)

select single_cost = round(cast(cost as float) / cast(participant as float),0)
squillman