SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select statement to reduce precision?


Select statement to reduce precision?

Author
Message
Mike Beckner
Mike Beckner
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 18

Hello all,

I'm sure there's a simple answer to this but it's escaping me. How can I reduce the precision of a money or smallmoney datatype in a SELECT statement so that it doesn't show 4 places to the right of the decimal?

I tried Select CAST(FIELDNAME) as float, but it will return only one decimal point to the right if the number is a whole number.

I want the result to ALWAYS return two decimal places.

Thanks


John Rowan
John Rowan
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5980 Visits: 4547

You're going down the right path, but try using decimal data type and setting the precision and scale. For example,

declare @money money

set @money = 150.2315

select CAST(@money as decimal(18,2))



John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Farrell Keough
Farrell Keough
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1588 Visits: 42

I'm not sure what you are going after. If this is a display issue, you may want to handle this in your front-end. Otherwise, I do not see the problems you are having and I would absolutely not use "float".

DECLARE @Money TABLE( Price money)
INSERT INTO @Money
SELECT 1
UNION
SELECT 1.01
UNION
SELECT 1.001
UNION
SELECT 2.5002
UNION
SELECT 2.5101
UNION
SELECT 3.001
UNION
SELECT 4.5202
UNION
SELECT 2.515
UNION
SELECT 2.5555

SELECT Price, CONVERT( decimal(5,2), Price) AS [ Price 5,2], CONVERT( float, Price) AS [ Price float], ROUND( Price, 2, 1) AS [ Price Round ]
FROM @Money



I wasn't born stupid - I had to study.
Mike Beckner
Mike Beckner
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 18

John Rowan,
Thanks for that...I forgot about the decimal type. Too bad it returns 0 instead of 0.00. I can handle that in code, though

Farrell,
Yeah, just a display issue. I like dealing with data issues, even if it's just display, in SQL when I can. I can change a stored procedure on the fly without having to stop debugging, change the code, then recompile.

Thanks to you both!

Mike


karri.vijay91
karri.vijay91
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 15
how to reduce precision value
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search