December 29, 2017 at 7:58 am
I'm need to convert kilograms to pounds and ounces. See attached data.
create table #T
(
ID,
Weight decimal(10,2)
)
insert into #T(ID,PtWeight) values(1,58.3)
insert into #T(ID,PtWeight) values(2,45.3)
insert into #T(ID,PtWeight) values(3,64.5)
insert into #T(ID,PtWeight) values(4,52.2)
insert into #T(ID,PtWeight) values(5,63.2)
insert into #T(ID,PtWeight) values(6,65.4)
insert into #T(ID,PtWeight) values(7,71.2)
insert into #T(ID,PtWeight) values(8,85.6)
insert into #T(ID,PtWeight) values(9,25.5)
insert into #T(ID,PtWeight) values(10,26)
insert into #T(ID,PtWeight) values(11,45.9)
insert into #T(ID,PtWeight) values(12,76.4)
December 29, 2017 at 8:10 am
Multiply by 2.2 to convert to pounds and multiply the resulting decimal part by 16 to get the ounces.
John
December 29, 2017 at 8:18 am
Or perhaps multiply by 2.2046226218, or some shorter variant of that, if you need more accuracy. 2.2 is rather rough estimate.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
December 29, 2017 at 8:23 am
ScottPletcher - Friday, December 29, 2017 8:18 AMOr perhaps multiply by 2.2046226218, or some shorter variant of that, if you need more accuracy. 2.2 is rather rough estimate.
Understood. I thought of looking up the exact number, but then I figured that the original poster is just as capable of doing that as I am.
John
December 29, 2017 at 8:25 am
John Mitchell-245523 - Friday, December 29, 2017 8:23 AMScottPletcher - Friday, December 29, 2017 8:18 AMOr perhaps multiply by 2.2046226218, or some shorter variant of that, if you need more accuracy. 2.2 is rather rough estimate.Understood. I thought of looking up the exact number, but then I figured that the original poster is just as capable of doing that as I am.
John
Idk, if OP was willing to do that, would prob never have asked the q to begin with 😀
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
December 29, 2017 at 8:32 am
Thanx but, how can I get the decimal part to multiply by 16. I think it has something to do with "%".
December 29, 2017 at 8:33 am
No, just subtract the integer part, which you get by using FLOOR.
John
December 29, 2017 at 8:48 am
Given a weight of 68.6 kg. Is this the best way to do it?
selectconcat(floor((68.6*2.2)),' lbs ',((68.6*2.2)%1)*16,'oz') as PtWeight
December 29, 2017 at 9:13 am
You don't want to do the calculation twice if you don't have to. Try this:
SELECT CONCAT(F.FloorWeight,'lbs ',(W.WeightinPounds - F.FloorWeight)*16,'oz') as PtWeight
FROM #T t
CROSS APPLY (
SELECT t.PtWeight * 2.2
) W(WeightinPounds)
CROSS APPLY (
SELECT FLOOR(W.WeightinPounds)
) F(FloorWeight)
John
December 29, 2017 at 10:05 am
Thanx. Happy New Year.
December 29, 2017 at 10:48 am
NineIron - Friday, December 29, 2017 8:48 AMGiven a weight of 68.6 kg. Is this the best way to do it?selectconcat(floor((68.6*2.2)),' lbs ',((68.6*2.2)%1)*16,'oz') as PtWeight
suggest you do as Scott suggested...increase accuracy....you are 5 ounces adrift
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
December 29, 2017 at 7:16 pm
Much easier solution is to simply weigh it on a pounds scale. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
December 29, 2017 at 7:35 pm
J Livingston SQL - Friday, December 29, 2017 10:48 AMNineIron - Friday, December 29, 2017 8:48 AMGiven a weight of 68.6 kg. Is this the best way to do it?selectconcat(floor((68.6*2.2)),' lbs ',((68.6*2.2)%1)*16,'oz') as PtWeight
suggest you do as Scott suggested...increase accuracy....you are 5 ounces adrift
More like 11 ounces adrift...
If you do the conversions using ounces to start with, the formulas become extremely symmetrical. Format it as you desire.
SELECT FLOOR(68.6*35.27396195/16), 68.6*35.27396195%16
--Jeff Moden
Change is inevitable... Change for the better is not.
December 30, 2017 at 4:50 am
Jeff Moden - Friday, December 29, 2017 7:35 PMJ Livingston SQL - Friday, December 29, 2017 10:48 AMNineIron - Friday, December 29, 2017 8:48 AMGiven a weight of 68.6 kg. Is this the best way to do it?selectconcat(floor((68.6*2.2)),' lbs ',((68.6*2.2)%1)*16,'oz') as PtWeight
suggest you do as Scott suggested...increase accuracy....you are 5 ounces adrift
More like 11 ounces adrift...
If you do the conversions using ounces to start with, the formulas become extremely symmetrical. Format it as you desire.
SELECT FLOOR(68.6*35.27396195/16), 68.6*35.27396195%16
Actually no, Jeff. It's about 5.07 oz out, which is much closer to the 5 oz J Livingstone suggested than to your 11. When I first read your comment, I thought there must be some crazy side-effect of the horrible truncation in decimal arithmetic because 2.2 lbs /kg is 35.2oz/kg and the error of about 0.074 oz/kg could only account for about being about 5 ox adrift. Then I looked at it a bit closer, and thought the number of operations and the number of decimal places are extremely low, that crazy truncation stuff can't have kicked in at all - so I decided the only answer was to run it - and your method delivers 151 pounds 3.793... oz while the method Nonelron asked about delivers 150 lbs 14.72 oz.
But I agree with you that it's generally cleaner to start with the smaller unit, so going for ounces first is good. And extra accuracy is definitely required unless answers are aceptable with a fifth of a percent error, so 35.27396195 is a good number to use (or 35.27396194958 for the really picky who have hardware and software supporting the latest decimal floating point standard, but I suppose none of us except some of the youngest are likely to see that).
Tom
December 30, 2017 at 6:36 am
TomThomson - Saturday, December 30, 2017 4:50 AMJeff Moden - Friday, December 29, 2017 7:35 PMJ Livingston SQL - Friday, December 29, 2017 10:48 AMNineIron - Friday, December 29, 2017 8:48 AMGiven a weight of 68.6 kg. Is this the best way to do it?selectconcat(floor((68.6*2.2)),' lbs ',((68.6*2.2)%1)*16,'oz') as PtWeight
suggest you do as Scott suggested...increase accuracy....you are 5 ounces adrift
More like 11 ounces adrift...
If you do the conversions using ounces to start with, the formulas become extremely symmetrical. Format it as you desire.
SELECT FLOOR(68.6*35.27396195/16), 68.6*35.27396195%16
Actually no, Jeff. It's about 5.07 oz out, which is much closer to the 5 oz J Livingstone suggested than to your 11. When I first read your comment, I thought there must be some crazy side-effect of the horrible truncation in decimal arithmetic because 2.2 lbs /kg is 35.2oz/kg and the error of about 0.074 oz/kg could only account for about being about 5 ox adrift. Then I looked at it a bit closer, and thought the number of operations and the number of decimal places are extremely low, that crazy truncation stuff can't have kicked in at all - so I decided the only answer was to run it - and your method delivers 151 pounds 3.793... oz while the method Nonelron asked about delivers 150 lbs 14.72 oz.
But I agree with you that it's generally cleaner to start with the smaller unit, so going for ounces first is good. And extra accuracy is definitely required unless answers are aceptable with a fifth of a percent error, so 35.27396195 is a good number to use (or 35.27396194958 for the really picky who have hardware and software supporting the latest decimal floating point standard, but I suppose none of us except some of the youngest are likely to see that).
It's my fault... I was incorrectly only looking at the number of ounces. Not sure why I didn't see the difference in pounds.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply