Friday, February 24, 2012

help

I am runnig query select count(*) from A
I like to display the count in following format. For ex.
If the count is 45 then it should display 000045
If the count is 145 then it should display 000145
If the count is 5 then it should display 000005
If the count is 0 then it should display 000000
Let know how is this possible.
Thanks in advance,
PeteTry this:
SELECT REPLICATE('0', 6 - LEN(cast(count(*) as varchar(10)))) +
cast(count(*) as varchar(10)) AS [Varchar Column]
from A
"peter" <peter@.discussions.microsoft.com> wrote in message
news:6C9199D5-320A-4064-80E1-3D71F6615EDE@.microsoft.com...
>I am runnig query select count(*) from A
> I like to display the count in following format. For ex.
> If the count is 45 then it should display 000045
> If the count is 145 then it should display 000145
> If the count is 5 then it should display 000005
> If the count is 0 then it should display 000000
> Let know how is this possible.
> Thanks in advance,
> Pete
>|||CREATE TABLE #ValueTable (value INT)
INSERT INTO #ValueTable
SELECT 1
UNION ALL
SELECT 500
UNION ALL
SELECT 4000
UNION ALL
SELECT 50000
UNION ALL
SELECT 00000
--Use RIGHT to pad the value
SELECT value, RIGHT('000000' + CONVERT(VARCHAR,value),6) AS
FormattedValue
FROM #ValueTable
drop table #ValueTable
http://sqlservercode.blogspot.com/

No comments:

Post a Comment