44

I have a column which is called studentID, but I have millions of records and somehow the application has input some arbitrary text in the column.

How do I search:

SELECT *
  FROM STUDENTS
 WHERE STUDENTID CONTAINS TEXT
4
  • 1
    Check this link. I assume that if you use CONTAINS, you are using Full text catalog. Commented Jun 26, 2013 at 14:08
  • 1
    Are you looking for "Text" or are you looking for any non-numeric text? It makes a difference to the question you're asking. Commented Jun 26, 2013 at 14:09
  • Other link that may be useful SQL Server LIKE vs CONTAINS. Commented Jun 26, 2013 at 14:15
  • When using TEXT as raw SQL, make sure it's escaped, preferably using prepared statements. Commented Dec 16, 2013 at 12:52

6 Answers 6

52

Leaving database modeling issues aside. I think you can try

SELECT * FROM STUDENTS WHERE ISNUMERIC(STUDENTID) = 0

But ISNUMERIC returns 1 for any value that seems numeric including things like -1.0e5

If you want to exclude digit-only studentids, try something like

SELECT * FROM STUDENTS WHERE STUDENTID LIKE '%[^0-9]%'
Sign up to request clarification or add additional context in comments.

1 Comment

ISNUMERIC returns 1/0, would have to be WHERE ISNUMERIC(STUDENTID) <> 1
35

Just try below script:

Below code works only if studentid column datatype is varchar

SELECT * FROM STUDENTS WHERE STUDENTID like '%Searchstring%'

Comments

14

Try LIKE construction, e.g. (assuming StudentId is of type Char, VarChar etc.)

  select * 
    from Students
   where StudentId like '%' || TEXT || '%' -- <- TEXT - text to contain

1 Comment

SqlServer uses a + as string concat operator, not a ||. msdn.microsoft.com/en-us/library/ms190301.aspx
4

riffing from bgs, please upvote them first.
I just wanted to expand on it

SELECT * FROM STUDENTS WHERE STUDENTID == 'Searchstring'

Will ONLY find Searchstring

SELECT * FROM STUDENTS WHERE STUDENTID like '%Searchstring%'

will find
1 Searchstring 1
2 Searchstring 2
3Searchstring3
etc Searchstring etc

SELECT * FROM STUDENTS WHERE STUDENTID like 'Searchstring%'

will find
Searchstring 1
Searchstring 2
SearchstringEtc
Will not find
1 Searchstring 1
or any prefixes at all

In this case % is used kinda the same as the same as the * wildcard, just for strings in this case.

Comments

2

Try this:

SElECT * FROM STUDENTS WHERE LEN(CAST(STUDENTID AS VARCHAR)) > 0

With this you get the rows where STUDENTID contains text

Comments

-2

Suppose STUDENTID contains some characters or numbers that you already know i.e. 'searchstring' then below query will work for you.

You could try this:

select * from STUDENTS where CHARINDEX('searchstring',STUDENTID)>0

I think this one is the fastest and easiest one.

Comments

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.