August 27, 2016 at 2:13 pm
i am getting numbers like as below,
1000000017777
1000000017342
how can i remove leading 1 and zeros (10000000) and get the result like 17777
August 27, 2016 at 3:44 pm
Assuming you know there is only one non-zero leading digit, doesn't matter how many zeroes there are
DECLARE @Number BIGINT;
SET @Number = 1000000017777;
SELECT CAST(RIGHT(@Number, LEN(@Number) - 1) AS INT);
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 27, 2016 at 11:13 pm
August 28, 2016 at 6:57 pm
Or if your number is actually character data. 😀
declare @number varchar(20) = '1000000017777'
select @number as before
select @number = RIGHT(@NUMBER,len(@number) - PATINDEX('%0[^0]%',@number))
select @number as After
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
August 29, 2016 at 6:59 am
If it's a bigint or similar, then a simple math operation will work.
DECLARE @Table TABLE( Somenumber bigint);
INSERT INTO @Table
SELECT 1000000017777 UNION ALL
SELECT 1000000017342;
SELECT *, Somenumber % 1000000000000
FROM @Table;
August 29, 2016 at 6:36 pm
dastagiri16 (8/27/2016)
i am getting numbers like as below,1000000017777
1000000017342
how can i remove leading 1 and zeros (10000000) and get the result like 17777
Just do what's requested:
remove leading 1
STUFF('1000000017777', 1,1,'')
and zeros
CONVERT(int, STUFF('1000000017777', 1,1,''))
_____________
Code for TallyGenerator
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply