Remove Leading Zeros in a Varchar Column



create table DatosTecnicos_test(id int, NumSerie varchar(25))
Insert into DatosTecnicos_test values (1,'00123'),(2,'A00456') ,(3,'00789') ,(4,'0000') ,(5,null)
 


--Update [DatosTecnicos]
--Set NumSerie = TRY_PARSE(NumSerie AS numeric(18,0))  

SELECT  NumSerie,   REPLACE(LTRIM(REPLACE(NumSerie, '0', ' ')), ' ', '0') AS NumSerie
FROM DatosTecnicos_test


Update DatosTecnicos_test
Set NumSerie= REPLACE(LTRIM(REPLACE(NumSerie, '0', ' ')), ' ', '0')

Select *   FROM DatosTecnicos_test
drop table DatosTecnicos_test



 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s