Avatar of KWDave
KWDaveFlag for United States of America

asked on 

MS SQL INT arithmatic to save result as varchar - always returns 0

I have values for height and weight stored in MS SQL 2008 as characters.

I need to calculate BMI, and store the result as varchar in the database.

The result always returns as 0. I tried declaring BMI as Float and Decimal and get the same result. I don't need the decimal part - the whole number result would be fine.

@BMI_NOTE is declared as VARCHAR(1000)
@Height and @Weight are declared as INT.

--Calculate BMI

SET @Height = CAST(@L1s3503 AS INT);
SET @Weight = CAST(@L1s3504 AS INT);

--Calculate BMI
--IF ((@L1s3504 IS NOT NULL) AND (@L1s3503 IS NOT NULL))
SET @BMI = ((@Weight/@Height/@Height) * 703) ;

--INSERT into NotesAnswers
SET @BMI_NOTE = 'BMI = ' + CAST(@BMI AS CHAR(4)) + ' Height = ' + CAST(@Height AS CHAR(4)) + ' Weight = ' + CAST(@Weight AS CHAR(4));
INSERT INTO NotesAnswer (Facility, Resident, InstrumentInstance, Note)
            VALUES(@Facility, @Resident, @InstrumentInstanceID, @BMI_NOTE);

The values for @Height and @weight are saved correct.

Thanks for any help
SQL

Avatar of undefined
Last Comment
KWDave
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of KWDave
KWDave
Flag of United States of America image

ASKER

Elegant and worked, with simpler syntax.

Thank you!
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo