Remove Zero

  • They all work fine.. some are just a tad faster... and easier to undertsand .

  • You are right Remi and once again thanks for your help! 

     

     

  • What is the need to reverse the string? How abt this?

    declare @STR varchar(32)

    select @STR  = '0010A'

    select(ltrim(replace('@str','0',' ')))

  • You're almost right .

    declare @STR varchar(32)

    select @STR = '0010A0'

    select (ltrim(replace(@str,'0',' ')))

    --1 A

    select REPLACE(ltrim(replace(@str,'0',' ')), ' ', '0')

    --10A0

  • What you think about a function?

    CREATE FUNCTION noleftzero(@word varchar(20))

    RETURNS varchar(20)

    AS

    BEGIN

     WHILE PATINDEX('0%', @word) = 1

     BEGIN    

        set @word = right(@word,len(@word)-1)

     END

       RETURN(@word)

    END

    go

    /*************/

    SELECT dbo.noleftzero('L0001AA') as nozero

    I'm not a expert but a think this is good.

    From a friend

  • CREATE FUNCTION noleftzero(@word varchar(20))

    RETURNS varchar(20)

    AS

    BEGIN

     WHILE PATINDEX('0%', @word) = 1

     BEGIN    

        set @word = right(@word,len(@word)-1)

     END

       RETURN(@word)

    END

    go

    /*************/

    SELECT dbo.noleftzero('L0001AA') as nozero

  • quoteYou seem to be the expert on paterns

    Who me! No way!

    Jack of all Trades, me, Master of None

    quoteDo you have a tutorial or a list of exemples of real life pattern searches (bol doesn't offer much in real life cases)

    Nope, fraid not. Took syntax from BOL and experimented a lot!

    Far away is close at hand in the images of elsewhere.
    Anon.

  • This function will run on each line which takes more than than writing it in the select. Secondly, David's version will run only once, therefore much faster than yours which will run once for each leading 0 + 1 :

    SUBSTRING([name],PATINDEX('%[^0]%',[Name]),LEN([Name]))

  • You're the only one who's continuously sending answers with pathindex and they more often than not beat the crap out of what anyone else can come up with. Maybe you should try to write a tutorial and publish it, or at least send it this way.

  • Expanding on David's solution, if you wanted to, you could remove the leading zeroes but keep spaces for these zeroes using the following:

    REPLICATE(' ', PATINDEX('%[^0]%', [Name]) - 1) + SUBSTRING([name],PATINDEX('%[^0]%',[Name]),LEN([Name]))

    Not usually what you want to do BUT there almost always seems to be a need for (at least once) for every scenario.

    hawg

    ----------------------------------------------------------01010011010100010100110000100000010100110110010101110010011101100110010101110010001000000101001001101111011000110110101101110011

Viewing 10 posts - 16 through 24 (of 24 total)

You must be logged in to reply to this topic. Login to reply