9

I need select only strings in my table, but this table has numbers and strings together.

Ex:

ID Name
1  Jacke11
2  Andre
3  Rodrigo11
4  55555

My select need return only Ids: 1, 2, 3.

Thanks

2
  • Eh, is id really a separate column and name a separate column? Commented Jan 12, 2012 at 18:18
  • if your select needs to return only 1, 2, 3, you need only integers from the table. Is separated by spaces? Commented Jan 12, 2012 at 18:18

2 Answers 2

18
SELECT ID
    FROM YourTable
    WHERE ISNUMERIC(Name + '.0e0') = 0
Sign up to request clarification or add additional context in comments.

4 Comments

I think I know why, but you can comment on why you're doing + '.0e0' as I don't think it's very intuitive to need that.
@Yuck - Because you can get unexpected matches with ISNUMERIC since it will evaluate TRUE for non-int datatypes like float (also money and some symbols). 12e10 will pass ISNUMERIC as TRUE. By adding the .0e0 you force it to check only for integer values.
@Yuck: Without that ISNUMERIC will return a 1 for such things as '+' and '$'.
Using ISNUMERIC for this will cause a lot more trouble. The most correct and performative answer at the moment is @JNK answer.
7

As an alternative to Joe's very fine ISNUMERIC solution, you can use PATINDEX to make sure you have an alpha character:

SELECT ID
    FROM YourTable
    WHERE PATINDEX('%[a-z]%', name) > 0

This may be slightly faster since it will stop searching the string as soon as it gets to the first alpha character.

1 Comment

to avoid an exception I would put a COALESCE(name, '') or something similar

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.