954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Difference of money in SQL Query..

Hi guys;
I need your assistance.
I am extracting some info from my database and I need difference between two fields of money values.
Lets call the fields as Field1,Field2,& Field3.

Field1 = Current Balance
Field2 = Starting Balance
Field3 = Field1 - Field2

From my query, I get Field3 as NULL. I cant understand what is wrong with my formula.

Here is the formula:

Declare @Field1 numeric (18,2),
        @Field2 numeric (18,2),
        @Field3 numeric (18,2)

Select Field1  = @Field1  , Field2 = @Field2
Select @Field3 = @Field1  - @Field2
Select @Field3


Please assist. TQ.

princekool
Light Poster
31 posts since Apr 2007
Reputation Points: 7
Solved Threads: 3
 

Look into parameterization with arithmetic operations.

Try this:

Select Field1 = CAST( @Field1 AS MONEY ), Field2 = CAST( @Field2 AS MONEY )
samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

Look into parameterization with arithmetic operations.

Try this:

Select Field1 = CAST( @Field1 AS MONEY ), Field2 = CAST( @Field2 AS MONEY )


Thanks Samaru for your contribution.
I tried your hint you gave me but still did not give the results i expected, now field3 is rounding off to a whole figure instead of actual difference.

For example if field1 = 50.35 and field2=20.12, field3 should be 30.23 but its giving 30.

Any ideas why its rounding off??

princekool
Light Poster
31 posts since Apr 2007
Reputation Points: 7
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: