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


How to trim the decimal places?


How to trim the decimal places?

Author
Message
Nuts
Nuts
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1213 Visits: 215
Hi Guys
I have a column with the figures which have 4 decimal points.
I want to trim them to two decimal points.
How do I do it??

Can it be done fron the reporting services as well??

Thanks
bitbucket-25253
bitbucket-25253
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34637 Visits: 25280
You could use the CONVERT function for example:

DECLARE @Num4 AS DECIMAL(10,4)
DECLARE @Num2 AS DECIMAL(10,2)
SET @Num4 = 1234.9876
SET @Num2 = CONVERT(DECIMAL(10,2),@Num4)
SELECT @Num2
@Num2 will then equal 1234.99 -- note the rounding which has taken place.
of course this works as well

DECLARE @Num4 AS DECIMAL(10,4)
DECLARE @Num2 AS DECIMAL(10,2)
SET @Num4 = 1234.9876
SET @Num2 = @Num4
SELECT @Num2
@Num2 will then equal 1234.99 -- note the rounding

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504959 Visits: 44238
Much simpler than all that... check out the ROUND function or the STR function (which also right justifies if you need that for a report or file). Smile

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
blessybaby
blessybaby
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 25
Pls try this

DECLARE @Num4 AS DECIMAL(10,4)
SET @Num4 = 1234.9876
SELECT STR(@Num4,Len(@Num4),2)
Madhivanan-208264
Madhivanan-208264
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4145 Visits: 476
Nuts (9/18/2008)
Hi Guys
I have a column with the figures which have 4 decimal points.
I want to trim them to two decimal points.
How do I do it??

Can it be done fron the reporting services as well??

Thanks

Post some sample data with expected result so that we dont need to guess what you really want



Madhivanan

Failing to plan is Planning to fail
manjunath5581
manjunath5581
SSC-Enthusiastic
SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)

Group: General Forum Members
Points: 176 Visits: 61
Hi,

I want to trim the value of decimal point to 2.

For example
1245.658797
123597.599945
798754.589785
787561.124657

I want this to be displayed as

1245.65
123597.59
798754.58
787561.12

If i try to use decimal(15,2) it will round off the value.

If i convert to type money then also the values get round off

The value type is real in the database

Please help...
arun.sas
arun.sas
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6293 Visits: 3493
hi,

try this,

create table #temp
(
amount decimal(15,6)
)

insert into #temp
select 1245.658797
union
select 123597.599945
union
select 798754.589785
union
select 787561.124657

select substring((cast(amount as char)),0,(charindex('.',(cast(amount as char)))+3)) amount from #temp
amount
1245.65
123597.59
787561.12
798754.58

ARUN SAS
MTV
MTV
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 72
DECLARE @Num4 AS DECIMAL(10,4)
SET @Num4 = 1234.982644
SELECT LEFT(CONVERT(VARCHAR(20),@Num4),CHARINDEX('.',CONVERT(VARCHAR(20),@Num4))+2)
manjunath5581
manjunath5581
SSC-Enthusiastic
SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)

Group: General Forum Members
Points: 176 Visits: 61
Hi Madhu,

When my value is like 1567987.4699987 i got the output as

1567987.47 which is actually rounding off the value

DECLARE @Num4 AS DECIMAL(10,4)
SET @Num4 = 1567987.4699987
SELECT LEFT(CONVERT(VARCHAR(20),@Num4),CHARINDEX('.',CONVERT(VARCHAR(20),@Num4))+2)

And the values specified above are just examples not real values.

Hope i am clear in my explaining the problem.
Ramesh Saive
Ramesh Saive
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14381 Visits: 2649
Using ROUND function...


SELECT CONVERT( NUMERIC(18,2), ROUND(1245.65879, 2, 1) )



--Ramesh


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