Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
in sql table two rows are given below
StartWeight    EndWeight      Charges    AddWeight     AddCharges
0.00           0.50           50            0              0
0.60           1.00           100           1             70


declare @Weight decimal
set @Weight=0.5
query select Charges from table where @Weight between StartWeight and EndWeight

First Issue) is this when @Weight is less than or equal to 0.4 it show 50 and when weight is 0.50 then it show 100 while i want to show 50 on 0.5

Second Issue) same as when @weight is 0.6 to 1.4 it show 100 while i want to show 100 only between 0.60 to 1.00 not on greater than 1.00

What I have tried:

declare @weight decimal,@result decimal
set @weight=0.50
set @result=(select Charges from [Customer].[CustomerCharges] where CustomerAccountID=1 and @weight between StartWeight and EndWeight)
select @result as Result

Result=100
Posted
Updated 30-Jan-18 2:08am
Comments
RedDk 29-Jan-18 13:35pm    
SELECT @@MAX_PRECISION AS 'Max Precision'

"p" sets precision
"s" sets scale

decimal[(p[ ,s])]

or

numeric[(p[ ,s])]

How can anyone usg the SQL BOL not know this?
Member 10028394 29-Jan-18 15:10pm    
I could not understand your answer, please give any example
Karthik_Mahalingam 30-Jan-18 2:21am    
Tip: use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

See Precision, scale, and length (Transact-SQL) | Microsoft Docs[^]

In SQL you define the precision and scale of a numeric value in the datbase. eg numeric(18,0) means you can store a 18 digit number but with no decimals where as Numeric(18,5) will allow 5 decimal spaces. Calculations will default to the field scales unless you explicitly convert them to a different precision / scale size.

ie
cast([StartWeight] as numeric(18,5))
 
Share this answer
 
When you declare a variable as a decimal without specifying the precision and scale it will default to decimal(18,0)
So when you in the next row set @weight=0.40 it will be rounded to a number that fits in the variable. In this case 0.

So change the first declaration to a decimal with the same size as startweight and endweight.

Like for example: declare @weight decimal(5,2),@result decimal(5,2)
 
Share this answer
 
Comments
Member 10028394 30-Jan-18 10:31am    
Thanks bro

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900