Sunday, March 25, 2012

Comparing Money

Hi,

I have a field in database money. When I enter value for it the amount entered is for example 20.000. How can I compare this value with noraml vaules i.e. like 20 in my search engine. Will I need to convert it to varchar and then compare it or is there some other way. Also if I need to convert it to varchar, how can I do it?

Thanks in advance,
UdayIt seems to me that you don't need to do any conversion. This code works as expected for me:


DECLARE @.myMoney Money
SELECT @.myMoney = 20.000

-- this returns Found
SELECT CASE WHEN @.myMoney = 20 THEN 'Found' ELSE 'Not Found' END

-- this returns Not Found
SELECT @.myMoney = 20.500
SELECT CASE WHEN @.myMoney = 20 THEN 'Found' ELSE 'Not Found' END

-- this returns Not Found
SELECT @.myMoney = 2.000
SELECT CASE WHEN @.myMoney = 20 THEN 'Found' ELSE 'Not Found' END

Or are you saying that in my second example you need to return Found?

Terri|||Hi,

What I want to do is if someone enter 20 and searches, all the value in money datatype with 20.000 should come out as a result.

Thanks in advance,
Uday.|||Maybe you should show us the code for this that you have that is not working the way you need. This way we can focus on your exact issue.

Terri|||Hi,

The code is as below:
select * from test where price='26' in which price is of datatype money and the value in the database is 26.0000. Hope you will be able to understand my problem.

Thanks in advance,
Uday|||If price is datatype money then you should not enclose 26 in single quotes. With the single quotes the database is forced to try to do an implicit conversion from varchar to money for the comparison, which it will not do. Remove the single quotes and you should have better luck.


select * from test where price=26

Terri|||Hi,

I tried that, it's working.

Thanks
Uday.

No comments:

Post a Comment