a little CASE help, please

Posted on 2005-04-28
Last Modified: 2010-03-19
Hi there.  I've got a proc that presently uses this CASE stmt to drop the last two characters of an ordernumber --- _B and/or _S.  I need to use very similar logic to do the same thing, but with one additional requirement.  this is my stmt:  

select case when charindex('_',ordernumber)>1 then left(ordernumber,charindex('_',ordernumber) -1) else ordernumber end as ordernumber
from database

so this:  000008915_B
becomes this:  000008915

very good.  but, i need to also drop the zeros that preface the 8915.  see, there is an unknown number of zeros prefacing the order number, i need them gone such that this:  000008915_B
becomes this:  8915

sometimes it's one, it's three or i said, it's an 'unknown' number of zeros. i just need to strip the prefacing zeros.

can anybody help me out w/this?
Question by:RLLewis
    LVL 14

    Accepted Solution

    select SUBSTRING(case when charindex('_',ordernumber)>1 then left(ordernumber,charindex('_',ordernumber) -1) else ordernumber end
    , PATINDEX('%[1-9]%', ordernumber), LEN(ordernumber)) as ordernumber
    from (Select '00008642_S' ordernumber) as t

    I used a Patindex to search for the first use of numbers [1-9], so everything else at the beginning other than a number 1 - 9 will get stripped off in the substring.
    LVL 1

    Author Comment

    oh fabulous adwise!  that is precisely what i needed.  thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now