10

What would be the most efficient way to check for numbers in a field that would contain a few letters? I would like to do this if the where statement as possible.

The data would look something like this:

3833N4323 32N907654 5W5840904

2
  • Are you checking whether numbers exist in the field? Do you wish to extract those numbers? Commented Jan 11, 2010 at 22:40
  • Just checking to make sure theres numbers in the field and will then selecting whats in there in its entirety. Commented Jan 11, 2010 at 22:59

3 Answers 3

23

Checking for at least one number in a field (corrected):

WHERE PATINDEX('%[0-9]%', field) != 0

Checking for only numbers in a field:

WHERE TRY_CONVERT(field AS int) IS NOT NULL
Sign up to request clarification or add additional context in comments.

4 Comments

how is this? AND PATINDEX('%[^0-9]%', pq.ReportNumber) > 1
You have the REGEXP keyword on SQL Server? What version of SQL Server is that?
This seems to work. AND PATINDEX('%[0-9^]%', pq.ReportNumber) > 1
The last block of code will not work. It needs to be changed to WHERE ISNUMERIC(field) = 1
10

A simple LIKE to find any number will suffice...

...WHERE LIKE '%[0-9]%'

Comments

3
select ISNUMERIC(data)

2 Comments

Unfortunately for me, this value: 33465,33465 , returns 1 ( number with a comma, which in my case I consider a string, not the kind of numbers I want to allow )
In 'my' Microsoft SQL Server 2019 version all of these return 1: select ISNUMERIC('$'), ISNUMERIC(','), ISNUMERIC('-'). Also select ISNUMERIC('123123123E1') -notice the E near the end- returns 1. However, ISNUMERIC('123123123F1') -now with an F- returns 0. It seems you cannot really rely on this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.