SQL: To search values in a table without a '#' character - sql

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.

Related

Replace multiple OR operators with a single IN or ANY operator

I would like to query a list of cities that end in vowels and I would like each city in the list to be distinct. The code noted below works fine but I would like to achieve the same thing using the IN or ANY operators.I have attached a picture of the description of the table I'm working with. Thank you!
SELECT DISTINCT City FROM Station
WHERE City LIKE '%a'
OR City LIKE '%e'
OR City LIKE '%i'
OR City LIKE '%o'
OR City LIKE '%u';
Table description
Use regexp
select DISTINCT
City FROM Station
WHERE regexp_like(city,'[aeiou]$')
You could use Oracle's REGEXP_LIKE here:
SELECT DISTINCT City
FROM Station
WHERE REGEXP_LIKE(City, '^.*[aeiou]$', 'i');
The regex pattern ^.*[aeiou]$ will match any city name which ends in a vowel. The third i parameter passed to REGEXP_LIKE tells Oracle to do the match in case insensitive mode, in case that would matter.
You can use regexp_like
SELECT distinct CITY
FROM station
WHERE regexp_like(CITY, '[aeiou]$', 'i');
Demo
If you want to use in, you can:
where substr(city, -1) in ('a', 'e', 'i', 'o', 'u')
You can use lower() if you need to handle upper-case as well.

How can I bring a register only if it doesn't contains a string - SQL?

My query is something like this:
SELECT * FROM table WHERE name LIKE "%Mary%"
But now, I want bring all the occurrences as long as it's not contains "Jane" especially in "Mary". I do not want "Mary Jane", neither "Jane Mary" or any variation (e.g. "Mary Smith Jane").
I really don't know how to.
EDIT:
I'm not sure if I can only use a "not like" because I'm already using "not like" in the same query for other reasons.
In fact:
SELECT * FROM table WHERE name NOT LIKE "%John%"
AND name NOT LIKE '%Charlie%'
AND name LIKE '%Mary%'
Just add that to the WHERE clause:
WHERE name LIKE '%Mary%' AND
name NOT LIKE '%Mary Jane%'
Or, if you mean that the exact match is not what you want:
WHERE name LIKE '%Mary%' AND
name <> 'Mary Jane'
SELECT *
FROM your_table
WHERE name LIKE '%Mary%'
and name <> 'Mary Jane'
In order to retrieve records that contain Mary in the string, but not Jane, you will want to keep your clause for LIKE '%Mary%' and add a clause for NOT LIKE '%Jane%'.
You can group these clauses together with parenthesis in order to isolate them from other clauses in the query.
SELECT *
FROM table
WHERE
(name LIKE '%Mary%'
AND name NOT LIKE '%Jane%')

Why LIKE returns no rows selected in sql?

I have this issue that's been bugging me for an hour.
Table:
--------------------------------------------
NAME | CHAR(10) |
SURNAME | CHAR(10) |
RANK | VARCHAR2(5)|
--------------------------------------------
I want to extract the NAME and the SURNAME if the surname ends with letter 'n'.
NOTES: LIKE doesn't work very well with char(10) so I converted that column to varchar2(10) , using :
ALTER TABLE students
MODIFY SURNAME VARCHAR2(10);
And I tried this:
SELECT NAME,SURNAME
FROM students
WHERE SURNAME LIKE '%n';
Unfortunately it gives me : no rows selected.
I'm thinking my column has spaces after last character ..
Still I don't know how my condition should look like.
I even tried calling to_char() function , it gives me the same output.
You probably have an issue with spaces in the data.
Try this:
SELECT NAME, SURNAME
FROM students
WHERE rtrim(SURNAME) LIKE '%n'
There might be a type casting on the column you mentioned so try.
Select name,surname
from students
where rtrim(upper(surname)) like '%N';
Try this as an alternative: WHERE CHARINDEX('n',surname) > 0

Check if a column contains text using SQL

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.

how can i write a sql query that finds rows where one column is a substring of another column

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