Home > Net >  Select first 2 characters and replace others
Select first 2 characters and replace others

Time:11-11

have dynamic numbers like this

33630521
4722829
36968

how can take first 2 numbers and replace others to 0 it can be varchar no problem want result like this

33000000
4700000
36000

tried this

declare @first2 varchar(2) = (SELECT SUBSTRING('36968795', 1, 2))
declare @others varchar(100) = (SELECT SUBSTRING('36968795', 3, 100))
select @first2   @others

but i stuck at replace @others to zeros because its unknown numbers may be anything

CodePudding user response:

It could be done using string functions as follows:

Select Concat(Left(@Var, 2),Replicate('0',Len(@Var)-2))

CodePudding user response:

You can use stuff

declare @x varchar(10)='123456789'

select Stuff(@x,3,10,Replicate('0',Len(@x)-2))
  • Related