December 12, 2011 at 8:39 am
Hi All,
How do i add a decimal? I have a value 123456 and i have to display it as 1234.56.
Please Help me solve this.
Thanks!
December 12, 2011 at 8:47 am
My Approach
select cast(cast(123456 as decimal(10,2))*0.01 as decimal(10,2))
Is there a simple way to do this?
December 12, 2011 at 8:49 am
You should leave the displaying of information to your presentation layer. If you must use T-SQL, this'll get you most of the way:
SELECT 123456 / 100.0
John
December 12, 2011 at 8:49 am
SELECT 123456 * 0.01
Returns
---------------------------------------
1234.56
December 12, 2011 at 8:51 am
select 123456 * 0.01
December 12, 2011 at 9:07 am
THANKS YOU EVRY1
December 13, 2011 at 12:18 am
Hi Kevin4u06,
You can write a function to solve your problem. This is function code:
CREATE FUNCTION [dbo].[format_currency] (@monetary_value decimal(20,2) ) returns varchar(20)
as
begin
declare @return_value varchar(20)
declare @is_negative bit
select @is_negative = case when @monetary_value<0 then 1 else 0 end
if @is_negative = 1
set @monetary_value = -1*@monetary_value
set @return_value = convert(varchar, isnull(@monetary_value, 0))
--------------------------------------------------------------------------------
declare @before varchar(20), @after varchar(20)
if charindex ('.', @return_value )>0
begin
set @after= substring(@return_value, charindex ('.', @return_value ), len(@return_value))
set @before= substring(@return_value,1, charindex ('.', @return_value )-1)
end
else
begin
set @before = @return_value
set @after=''
end
-- after every third character:
declare @i int
if len(@before)>3
begin
set @i = 3
while @i>1 and @i < len(@before)
begin
set @before = substring(@before,1,len(@before)-@i) + ',' + right(@before,@i)
set @i = @i + 4
end
end
set @return_value = @before + @after
if @is_negative = 1
set @return_value = '-' + @return_value
return @return_value
end
kevin4u06 (12/12/2011)
Hi All,How do i add a decimal? I have a value 123456 and i have to display it as 1234.56.
Please Help me solve this.
Thanks!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply