Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Find last non-zero digits in a Number

Nov 5 2012 12:00AM by Madhivanan   

There was a question in the forums where a user asked for a solution to find out the number which is succeeded by the last 0 in that number

For example, in numbers like 10807023 and 1000508, 23 and 8 are the numbers which are succeeded by the last 0 of that number. There can be several solutions and here is the one.

declare @t table(num int)
insert into @t
select 1000002 union all
select 1000032 union all
select 400073 union all
select 19802 union all
select 52 union all
select 502065 
select 
	num,
	right(num,case when string_len=0 then total_len else string_len-1 end) as new_num 
from
(
	select 
		num,
		len(num) as total_len,
		patindex('%[0]%',cast(reverse(num) as varchar(100))) as string_len 
	from 
		@t
) as t

The result is
num         new_num
----------- ------------
1000002     2
1000032     32
400073      73
19802       2
52          52
502065      65

Tags: 


Madhivanan
3 · 40% · 12935
4
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

10  Comments  

  • Is this just an abstract problem or does it come from the real world somewhere? I would be curious to know.

    commented on Nov 5 2012 2:50AM
    dishdy
    16 · 10% · 3269
  • I do not remember the link for this question however the user specified it is a real world problem where they need to make some decisions based on the the last non-zero digits. My guess is that this may be a flag where each non-zero digits indicate some state and they are seperated by zero.

    commented on Nov 5 2012 3:14AM
    Madhivanan
    3 · 40% · 12935
  • My Answer to this is

    SELECT REVERSE(SUBSTRING(CAST(REVERSE(num) as varchar(max)), 1,  CHARINDEX('0',REVERSE('0'+ cast(num as varchar(max))))-1))   from @t
    
    commented on Nov 5 2012 4:28AM
    Jeetendra
    145 · 1% · 342
  • I'd use the following:

    select ltrim(right(replace(num,'0',space(20)),20)) from @t

    It doesnt have to be 20 spaces. At most len(num).

    commented on Nov 6 2012 1:36PM
    Leonid Koyfman
    50 · 4% · 1183
  • Leonid. Yes, that's a nice one. I don't think anyone will make this any shorter - measured in characters.

    commented on Nov 6 2012 2:27PM
    dishdy
    16 · 10% · 3269
  • Leonid Koyfman, thats very nice solution.

    commented on Nov 7 2012 1:07AM
    Madhivanan
    3 · 40% · 12935
  • Leonid, I have learned. Thanks a lot. It is one of a good technique

    commented on Nov 7 2012 11:46AM
    Jeetendra
    145 · 1% · 342
  • @dishdy, you challenged me. I'm not a big proponent of implicit conversion, but measured in charachters this one is shorter:

    select 1*(right(replace(num,'0',space(20)),20))from @t

    commented on Nov 9 2012 12:29PM
    Leonid Koyfman
    50 · 4% · 1183
  • Leonid, you can get rid of a pair of parenthesis from your new solution as follows:

    select 1*right(replace(num,'0',space(20)),20)from @t

    I like the way you got rid of that space before 'from'.

    commented on Nov 9 2012 1:42PM
    dishdy
    16 · 10% · 3269
  • @dishdy ... and apostrophes around 0.

    select 1*right(replace(num,0,space(20)),20)from @t

    I believe that's it.

    BTW. I learnt about space before 'from' from Madhivanan.

    commented on Nov 9 2012 1:58PM
    Leonid Koyfman
    50 · 4% · 1183

Your Comment


Sign Up or Login to post a comment.

"Find last non-zero digits in a Number" rated 5 out of 5 by 4 readers
Find last non-zero digits in a Number , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]