June 3, 2015 at 5:22 am
I'm very new to sql and it's syntax (access users), I've played around with various combinations, how would I get the weight to display as pounds and ounces? the field type is varchar
This works to convert, but number doesn't display the way I want
(dbo.BIRTH_WT_NUM / 0.035274) AS [Birth weight in grams]
June 3, 2015 at 5:40 am
help us help you!
CREATE TABLE and sample data get you help fast!
i put this example together that might help you visualize it.
i got the formula for ounces to grams from google.
CREATE TABLE #MySampleData(gramweight decimal (19,6))
INSERT INTO #MySampleData
SELECT 3175.15 UNION ALL
SELECT 3000.15 UNION ALL
SELECT 5172.15 UNION ALL
SELECT 4200.15
/*
TotalOunces gramweight Pounds Ounces
---------------- ------------ ------- -----------
112.03775582215 3175.150000 7 0
105.86273817925 3000.150000 6 9
182.50352858151 5172.150000 11 6
148.20571630204 4200.150000 9 4
*/
;WITH MyCTE
AS
(
SELECT (gramweight / 28.34 ) AS TotalOunces,gramweight
FROM #MySampleData
)
SELECT TotalOunces,gramweight,
CONVERT(INT,TotalOunces) / 16 As Pounds ,
CONVERT(INT,TotalOunces) % 16 AS Ounces
FROM MyCTE
Lowell
June 3, 2015 at 6:04 am
The next question would be, do you understand how and why Lowell's solution works?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply