Check if a column contains text using SQL

2019-06-15 07:35发布

问题:

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

回答1:

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]%'


回答2:

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

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


回答3:

Just try below script:

Below code works only if studentid column datatype is varchar

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


回答4:

Try this:

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

With this you get the rows where STUDENTID contains text