Thursday, March 22, 2012

Converting 1000 into 10.00

I have been given a Product table whoes all column types are varchar(8000)

One of the column is Price and other is DecimalPosition. Price column includes price without any decimal place and the data in DecimlaPosition column determins where the decimal should be placed.

So for instance, if the Price column includes '1000' and DecimalPosision includes '2' >> then it means that the actual price for this product is '10.00' and NOT '1000'. Similarly, if the DecimalPosision includes '3' >> then it means that the actual price for this product is '1.000' and NOT '1000'
My question is that when I am getting the price for a product from this table, how can I get the price in the correct format, e..g like '10.00' and not '1000'
Should I use SQL statements to convert 1000 into 10.00 or should I use some sort of programming logic to convert 1000 into 10.00.
kind regards

select substring(price,1,decimalPosition) + '.' + substring(price, 1 + decimalPosition, len(price) - decimalPosition)
from YOURTABLE

Nick

Edit: include decimal|||

There's a slight problem in Nick's code. It should be:

select substring(price, 1, len(price) - decimalPosition) + '.' + substring(price, len(price) - decimalPosition + 1, decimalPosition) as col1

However, no matter how you argue, that is just some flawed design. What I would strongly recommend is set up a migration "roll-out" plan for the data that is already in production and convert them to a standard price structure where you drop that decimalPlace column and correct the Price column into an real number.

Cheers,

Justin

No comments:

Post a Comment