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?

Example:

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
JedidiaAsked:
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.

knightEknightCommented:

where isnumeric(left(myColumn,3))=1
   and isnumeric(left(myColumn,4))=0
0
knightEknightCommented:
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
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

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

TempDBACommented:
SELECT * FROM <TableName>
WHERE <ColumnName> like '[0-9][0-9][0-9][^0-9]%'
0
JedidiaAuthor Commented:
KnightEknight and Sharath, thank you both for your input.  Cutting and pasting each of your inputs worked.
0
knightEknightCommented:
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).
0
knightEknightCommented:
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'
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.