|
|
Method 1 : CAST to Bigint
declare @s varchar(100)
set @s ='0000004007340007402100'
select cast(@s as bigint)
CASTing the string to Bigint will automatically delete the leading zeroes

Method 2 : Use Replace function
declare @s varchar(100)
set @s ='0000004007340007402100'
select replace(ltrim(replace(@s,'0',' ')),' ','0')
The T-SQL code shown above first replaces all zeroes to a single space. The LTRIM function deletes all leading spaces and the second replace function, replaces all spaces to zeroes, so that all leading zeroes are deleted.
OUTPUT
Did you like this post?
|
|
|
||
|
|
|
|
|
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |






comments
2 Responses to "Deleting Leading Zeros in a String (SQL)"SERIOUSLY !!
What if the string does not contains only digits? Can this computer science breakthrough be applied to "1" removal with some little modifications?
Post a Comment