Wednesday, March 26, 2014

How to prefix 0 to make same length string for column.

1. There’s a table with below data.

Write a query which will give below output. 

declare @employee as table(
id int
insert into @employee values(1)
insert into @employee values(20)
insert into @employee values(300)
insert into @employee values(4000)
insert into @employee values(50000)
insert into @employee values(600000)

select * from @employee

select replicate('0',6-len(id)) + cast(id as varchar) from @employee

No comments:

Post a Comment