Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 int to decimal

Author  Topic 

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2009-01-03 : 11:00:28
Hello

how can i retrieve int field as 2 decimal places like (1985 should be 19.85)

thanks in advance

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2009-01-03 : 12:10:51
DECLARE @SomeInt INT
SET @SomeInt = 1985

SELECT CAST(@SomeInt/100.0 AS DECIMAL(10,2))

The specified decimal place on the 100 is necessary, otherwise you'll get integer division and a result of 19.00

--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-03 : 13:55:55
quote:
Originally posted by GilaMonster

DECLARE @SomeInt INT
SET @SomeInt = 1985

SELECT CAST(@SomeInt/100.0 AS DECIMAL(10,2))

The specified decimal place on the 100 is necessary, otherwise you'll get integer division and a result of 19.00

--
Gail Shaw
SQL Server MVP


whats the purpose of final CAST? wont @SomeInt/100.0 be enough?
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2009-01-03 : 15:23:00
quote:
Originally posted by visakh16


whats the purpose of final CAST? wont @SomeInt/100.0 be enough?



To get the final display to have only 2 decimals. Without it, it returns 19.850000

It could be left out if the result is not going to be displayed directly.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

thiyagu_rind
Starting Member

46 Posts

Posted - 2009-01-23 : 00:33:30
Yes, we have to use the CAST at last step.
By the below casting also you can achieve this.


DECLARE @SomeInt INT
SET @SomeInt = 1985
SELECT CAST(@SomeInt/100.0 AS FLOAT)


Regards
Thiyagarajan
www.sqlhunt.blogspot.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-23 : 00:53:15
quote:
Originally posted by thiyagu_rind

Yes, we have to use the CAST at last step.
By the below casting also you can achieve this.


DECLARE @SomeInt INT
SET @SomeInt = 1985
SELECT CAST(@SomeInt/100.0 AS FLOAT)


Regards
Thiyagarajan
www.sqlhunt.blogspot.com


better to use decimal or numeric rather than float as it gives only an approximate value
Go to Top of Page
   

- Advertisement -