Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Format values as Percent


Format values as Percent

Author
Message
Quatrei.X
Quatrei.X
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 466
Hi there,

If your already using the code given byu Gila Monster (BigGrinYOUR THE MAN!), you can add this...

Yeah I know the code I added looks icky but hope it helps


Select NewColumn=CASE
WHEN CAST(LEFT(MyColumn,Len(MyColumn)-1)AS DECIMAL(5,2))<10 THEN '0'+CAST(MyColumn AS VARCHAR(50))
ELSE MyColumn
END
FROM
(
SELECT CAST(CAST(0.10*100 AS numeric(10,2)) AS varchar(5)) + '%' as MyColumn
Union
SELECT CAST(CAST(0.011*100 AS numeric(10,2)) AS varchar(5)) + '%' as MyColumn
Union
SELECT CAST(CAST(0.02*100 AS numeric(10,2)) AS varchar(5)) + '%' as MyColumn
Union
SELECT CAST(CAST(0.12*100 AS numeric(10,2)) AS varchar(5)) + '%' as MyColumn
) MyTable
Order by CAST(Replace(MyColumn,'%','') as DECIMAL) asc




_____________________________________________
Quatrei Quorizawa
SmileBigGrinTongueWinkw00tCoolHehe
MABUHAY PHILIPPINES!

"Press any key...
Where the heck is the any key?
hmmm... Let's see... there's ESC, CTRL, Page Up...
but no any key"
- Homer Simpson

Quatrei.X
Quatrei.X
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 466
Hi there... agian,

Remember the icky code I added to Gila Monster and Dugi's code? We'll I made it a less icky but if your already using Gila's code, you need a few modifications for this one


Select NewColumn=CASE
WHEN MyColumn<10 THEN '0'+CAST(MyColumn AS VARCHAR(50))+'%'
ELSE CAST(MyColumn AS VARCHAR(50)) +'%'
END
FROM
(
SELECT CAST(0.10*100 AS numeric(10,2)) as MyColumn
Union
SELECT CAST(0.011*100 AS numeric(10,2)) as MyColumn
Union
SELECT CAST(0.02*100 AS numeric(10,2)) as MyColumn
Union
SELECT CAST(0.12*100 AS numeric(10,2)) as MyColumn
) MyTable
Order by CAST(Replace(MyColumn,'%','') as DECIMAL) asc



Oh... and sorry for my bad English, Hope my code also helps ^__^

_____________________________________________
Quatrei Quorizawa
SmileBigGrinTongueWinkw00tCoolHehe
MABUHAY PHILIPPINES!

"Press any key...
Where the heck is the any key?
hmmm... Let's see... there's ESC, CTRL, Page Up...
but no any key"
- Homer Simpson

Quatrei.X
Quatrei.X
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 466
Hi there... again,

Heres another less icky way


Select RIGHT('00'+CAST(MyColumn AS VARCHAR(50))+'%',6)
FROM
(
SELECT CAST(0.10*100 AS numeric(10,2)) as MyColumn
Union
SELECT CAST(0.011*100 AS numeric(10,2)) as MyColumn
Union
SELECT CAST(0.02*100 AS numeric(10,2)) as MyColumn
Union
SELECT CAST(0.12*100 AS numeric(10,2)) as MyColumn
) MyTable
Order by CAST(Replace(MyColumn,'%','') as DECIMAL) asc



Although if you use this code, make sure that you won't have a data with0 '100%' or 1.00 casue that would ruuin the could like so


Select RIGHT('00'+CAST(MyColumn AS VARCHAR(50))+'%',6)
FROM
(
SELECT CAST(1.00*100 AS numeric(10,2)) as MyColumn
) MyTable
Order by CAST(Replace(MyColumn,'%','') as DECIMAL) asc



Hope this also helps

_____________________________________________
Quatrei Quorizawa
SmileBigGrinTongueWinkw00tCoolHehe
MABUHAY PHILIPPINES!

"Press any key...
Where the heck is the any key?
hmmm... Let's see... there's ESC, CTRL, Page Up...
but no any key"
- Homer Simpson

nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
Quatrei.X (8/28/2008)
Hi there,

If your already using the code given byu Gila Monster (BigGrinYOUR THE MAN!), you can add this...



By the way, Gila Monster is female !!!

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Quatrei.X
Quatrei.X
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 466
AAAAAAAAW... Sorry about that gila. Didn't see the picture perfectly

sorry for bad english

_____________________________________________
Quatrei Quorizawa
SmileBigGrinTongueWinkw00tCoolHehe
MABUHAY PHILIPPINES!

"Press any key...
Where the heck is the any key?
hmmm... Let's see... there's ESC, CTRL, Page Up...
but no any key"
- Homer Simpson

Calibear
Calibear
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 132
Don't know if you are still looking for an answer, but this should work for you:

Select MyColumn FROM
(
SELECT right(replicate('0',5) +CAST(CAST(0.10*100 AS numeric(10,2)) AS varchar(5)), 5) + '%' as MyColumn
Union
SELECT right(replicate('0',5) +CAST(CAST(0.011*100 AS numeric(10,2)) AS varchar(5)), 5) + '%' as MyColumn
Union
SELECT right(replicate('0',5) +CAST(CAST(0.02*100 AS numeric(10,2)) AS varchar(5)), 5) + '%' as MyColumn
Union
SELECT right(replicate('0',5) +CAST(CAST(0.12*100 AS numeric(10,2)) AS varchar(5)), 5) + '%' as MyColumn
) MyTable
Order by CAST(Replace(MyColumn,'%','') as DECIMAL) asc

--------
For SQL Tips, check out my blog: http://lantztechknowledge.blogspot.com/
You can also follow my twitter account to get daily updates: @BLantz2455
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