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)
 Percentage Functionality in t-SQL

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-06-03 : 05:26:41

Dear All,

I need to implement the percentage functionality in T-SQL which is very similar to Excel 2007 where i can round off and display proper value.


And also need to concatenate the % symbol to it,pls help me.

Input Output
Ex: -4.0283739395038591 --> -4%
9.8039215686274508 --> 1%
-6.358128786459309 --> -6%

Thanks,
Gangadhara MS
SQL Developer and DBA

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-03 : 05:40:35
CONVERT(varchar(10),convert(int,round(fld/10,0)))+'%'

Why are you doing this in sql? the conversion maybe but adding the %?

umm - doesn't work.
Not sure what you want - just the first significant digit followed by %?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2011-06-03 : 05:44:17
agree, the concatenation of "%" should happen at the presentation layer. Assuming there is a presentation layer

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-03 : 05:48:27
The 9 becoming 1 rather than 10 means that I think you will have to have a case statement to deal with negative numbers.
What would 99 become - the same as 9?


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-03 : 05:54:38
I think this works for the examples you have given
declare @fld decimal(18,10) = -4.0239215686274508
select case when @fld < 0 then '-' else '' end + left(replace(CONVERT(varchar(10),convert(decimal(18,0),@fld,0)),'-',''),1) + '%'

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -