Check for values that start with alpha

Posted on 2011-10-07
Last Modified: 2012-05-12
I have this regex and i swear it was working but now it doesnt. I have order numbers like
WS0000095-1 OR WS0000095 (no dash). I will also have other order numbers like
3199888111 or 99900011-1 (just numbers or numbers with dash)

How can I check if an order number starts with alpha?

declare @orderNum varchar(15)
set @ordernum = 'WS0000095-1'

if (@orderNum LIKE '[A-Za-z][A-Za-z][A-Za-z]%')
      select 'test'
Question by:Camillia
    LVL 28

    Expert Comment

    dbo.RegexMatch( @orderNum, N'^[A-Za-z]' )
    LVL 17

    Accepted Solution

    Your regex is testing the first 3 characters when it looks like you will only ever have the first 2 characters as alpha.

    If you want to just test the first two characters for alpha, change your regex to "[a-zA-Z][a-zA-Z]%"
    LVL 7

    Author Comment

    I can do this, correct?
    .... and  @ordernum not LIKE '[a-zA-Z][a-zA-Z]%'

    in my sql statement..want to make check if an order number doesnt start with alpha and yes, i just need to check the first 2 digits for alpha
    LVL 28

    Expert Comment

    NOT dbo.RegexMatch( @orderNum, N'^[A-Za-z]{2}' )

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now