T-SQL Pattern Matching

I am trying to return records where the first three characters in the column start with numbers only.  If it has four numbers, I want it to skip.  Any thoughts?


234 <--returned in results
323 <--returned in results
2345 <- NOT returned in results
234H <--returned in results
265 <--returned in results
543 T <--returned in results
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


where isnumeric(left(myColumn,3))=1
   and isnumeric(left(myColumn,4))=0
oops, I didn't see the space in the last line above, try this instead:

where isnumeric(left(myColumn,3))=1
  and isnumeric(SUBSTRING(myColumn,4,1))=0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SharathData EngineerCommented:

This query works but I think knightEknight's query is more elegant.
select * 
  from your_table 
 where isnumeric(left(column_name,4)) = case when len(left(column_name,4)) = 4 then 0
                                                               else isnumeric(left(column_name,4)) end

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

SELECT * FROM <TableName>
WHERE <ColumnName> like '[0-9][0-9][0-9][^0-9]%'
JedidiaAuthor Commented:
KnightEknight and Sharath, thank you both for your input.  Cutting and pasting each of your inputs worked.
I want to give a shout-out to TempDBA's solution as well.  I believe his is the most efficient because it does not use in-line functions, and it may allow the optimizer to make use of an index (if it exists) on the column (under ideal conditions anyway).
To clarify, the index could be used if you also include additional conditions on the column like this:

SELECT * FROM <TableName>
WHERE <ColumnName> like '[0-9][0-9][0-9][^0-9]%'
  and <ColumnName> >= '000'
  and <ColumnName> <= '999'
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.