Reputation: 131
I have query similar to this:
DECLARE @value decimal(8,0) = 1
SELECT (CAST @value AS varchar(8))
How can I get output formatted with leading zeros (00000001, 00000023, 00000623
etc.)?
How can I do that?
It is simple task in .Net or Java, but I must do it inside view.
Upvotes: 1
Views: 667
Reputation: 24046
try this:
declare @value varchar(8)='623';
Select ltrim(right(replicate(0,8) + CAST (@value AS varchar(8)),8))
Upvotes: 2
Reputation: 79889
You can use REPLICATE
and RIGHT
to do this, like so:
SELECT RIGHT(REPLICATE('0', 8) + CAST(@valueAS VARCHAR(8)), 8)
Upvotes: 1
Reputation: 735
Try this
SELECT RIGHT('00000000' + CAST (@value AS varchar(8)),8)
Upvotes: 2
Reputation: 13486
try this:
DECLARE @value decimal(8,0) = 1
SELECT REPLICATE('0',8-len(@value))+CAST(@value AS varchar(8))
Upvotes: 1
Reputation: 8832
This should work:
DECLARE @value decimal(8,0) = 1
SELECT RIGHT('0000000' + CAST(@value AS varchar(8)), 8)
Upvotes: 2