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
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]%'
Just try below script:
Below code works only if studentid column datatype is varchar
SELECT * FROM STUDENTS WHERE STUDENTID like '%Searchstring%'
Try LIKE construction, e.g. (assuming StudentId is of type Char, VarChar etc.)
select *
from Students
where StudentId like '%' || TEXT || '%' -- <- TEXT - text to contain
Try this:
SElECT * FROM STUDENTS WHERE LEN(CAST(STUDENTID AS VARCHAR)) > 0
With this you get the rows where STUDENTID contains text
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.
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.
Related
Here is what I am trying to do:
select * from Person where CONTAINS((fName,sName),'John AND Doe')
So I am trying to search for John Doe but I get that I cannot use AND here (just showing my chain of thought) so how can I search for John in the fName and Doe in the sName ? But I don't want to use "Contains" twice like this:
SELECT * FROM Person
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');
Since we can have
(fName,sName)
but I cannot use
'John','Doe'/'John' AND 'Doe'
Your statement
SELECT * FROM Person
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');
can't compile because CONTAINS returns a number, not a boolean.
It shall respond,on oracle, with this kind of error
ORA-00920: opérateur relationnel non valide
00920. 00000 - "invalid relational operator"
(the relational operator error's cause is not the AND's existence, it's because you try to AND two numbers)
What do you intend to do? If you want to select the line in the person table whose fName columns contains the substring John and whose sName column contains the substring Doe, you may use the like operator, which uses % as a wildcard.
SELECT * FROM Person
WHERE fName LIKE '%John%'
and sName LIKE '%Doe%'
I don't practice much the CONTAINS method, but if you desire to use it, according to documentation, you should use something like
SELECT * FROM Person
WHERE CONTAINS(fName, 'John') > 0
and CONTAINS(sName,'Doe') > 0
If you really don't want to use the AND operator (for whatever obscure reason like proving a sql injection filter is bad), you can use this trick : compare a concatenation of the 2 columns and the like operator like so
SELECT * FROM Person
WHERE fName || sName like '%John%Doe%';
but this last trick will also match the line where fname is John Doe and sName is Jean-Michel :)
This should work:
SELECT * FROM dbo.Person
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');
But in your case you dont want to use 2 contains so:
Alternatively, you could add a new computed column with a full text index on it.
Add a column like this:
computedCol AS fName+ ' ' + sName
And create the full text index:
CREATE FULLTEXT INDEX ON Person(computedCol LANGUAGE 1033)
KEY INDEX pk_Person_yourPrimaryKeyName
Then you can do this:
SELECT * FROM dbo.Person
WHERE CONTAINS(*, 'John AND Doe')
Hope it works.
I tried :
Use SchoolDB
Select Distinct Surnames from Students
where Surname LIKE '%^#%'
above I get zero values.
I also tried replacing '%^#%' with '%[!#]%' or '%[^#]%'
SELECT
DISTINCT Surnames
FROM Students
WHERE
Surname NOT LIKE '%#%'
You can use NOT LIKE, like this:
where Surname NOT LIKE '%[#]%'
Expect it helps.
My Products table has almost 10,000 records where i want the "artist" ( varchar ) column name to sort by Firstname and Lastname. In the where clause i am sending initial letter of either first name or lastname. This Query works for few records but not for all, its probably because of the pattern of the records ?
Here is my query
SELECT * FROM Products WHERE SUBSTRING(artist, 1, 1) Like 'C'
Doesnt return the records... the way i want it ...Few of the records of the table ( artist) column are below
Comtesse Mathilde duMonceau de Bergendael
Mulholland, S.J.
Cleminson
NULL
Samuel Jackson
Your LIKE clause is wrong. Also you should not be using LIKE at all here, as you're only filtering by one letter:
.... WHERE LEFT(artist, 1) = 'C'
EDIT LEFT is actually faster than SUBSTRING.
Also, you can not expect anything that's not starting with a C to be returned, as you're explicitly only asking for entries that start with a C.
I guess what you really need is the following:
.... WHERE LEFT(FirstName, 1) = 'C' OR LEFT(LastName, 1) = 'C' ORDER BY LastName, Firstname
If you only have the Artist column, things get complicated, as you actually do need to use LIKE, which makes things pretty slow
.... WHERE Artist LIKE `%C%`
returns any artist, the name of which contains a C anywhere in the name. This would also return these:
Clemens Test
Test Cutler
Arachnophobia
User this where clause
where left(artist, 1) = 'C'
or
where artist like 'C%'
How would I use a IN table with like? So that I could use % in them? By in I mean:
SELECT fields
FROM table
WHERE age = "50"
AND name IN ("tim", "bob", "nancy", "john");
I already tried:
SELECT fields
FROM table
WHERE age = "50"
AND name LIKE ("2010-09-17%", "2010-09-16%")
But it gave the error "Operand should contain 1 column(s)"
You can use a number of LIKE expressions:
SELECT fields
FROM table
WHERE age = "50"
AND (
name LIKE "2010-09-17%"
OR name LIKE "2010-09-16%"
);
or you can use a regex:
SELECT fields
FROM table
WHERE age = "50"
AND name REGEXP "2010-09-17.*|2010-09-16.*";
or, cleverly
SELECT fields
FROM table
WHERE age = "50"
AND name REGEXP "2010-09-1(6|7).*";
There is no combination of the LIKE and IN clauses. It's either one, or the other, syntax:
SELECT fields
FROM table
WHERE age = 50
AND ( name IN ('tim', 'bob', 'nancy', 'john')
OR name LIKE '2010-09-17%'
OR name LIKE '2010-09-16%')
The alternative to consider when searching text is Full Text Search (FTS):
SELECT fields
FROM table
WHERE age = 50
AND MATCH(name) AGAINST('tim bob nancy john')
...but this requires MyISAM tables, and Full Text Indexing.
Put the values in a table (MyParamsTable) and use LIKE in a JOIN condition e.g. something like:
SELECT fields
FROM table
INNER JOIN MyParamsTable
ON table.name LIKE CONCAT(MyParamsTable.name, "%")
WHERE age = "50";
I wish to find all rows in a table where one column is a substring of another column.
In other words, suppose I have a table (called people) with two columns: firstname and lastname, and I want to find all people like "rob robinowitz" and "jill bajillion".
Is there a way to do something like "select * from people where lastname like %firstname%"? (But something which actually works).
You were close
select * from people where lastname like '%' + firstname + '%'
Alternative way (may be even faster)
select * from people where charindex(firstname,lastname)>0
If you are using MySQL you could
SELECT * FROM people WHERE INSTR(lastname, firstname) <> 0