Oracle GroupBy of worlds from text field - sql

I have a table of
Full Name - varchar2(200)
Age - Number
can I get the count of name for example that inside the textfield
and I dont mean to Select Count(*) from table;

not sure about the requirements but I guess this is what you want
select count(*), name from table group by name;

What is the "textfield"? A bind variable? A value in a column in a different table? The standard solution is
select count(*) from <your_table_name> where <textfield> like '%' || "First Name" || '%' ;
I used double-quotes around First Name - that is the only way column names in Oracle may contain spaces (but I hope you didn't actually do that, it is a very poor practice). Also, if there is the risk that "Jackson" appears in the textfield and you don't want that to count as "Jack" (first name), then replace the last '%' with ' %' (single-quote, SPACE, %, single-quote).
This will give the total number of names from your table that are present in the text field. It will not count duplicates (if John appears five times, it will still be only counted once). If this is not your requirement, please state your requirement more clearly. For example, you may instead want to show how many times each of the names in your table appears in the textfield... or any number of other possible interpretations. What do you really need?

Related

ways to check for invalid characters? oracle sql

Looking for ways to filter out special signs, letters etc. from studentID in oracle SQL and show those records as invalid.
What is the best way to filter out letters a-Z and other characters? (only leaving numbers)
SELECT replace(Translate(studentid,'a-Z<>!-\+=/&', '??'),'?','') as StudentID, 'Invalid Characters in Student ID'
FROM students
The simplest approach is to use regular expressions. For example
select studentid
from student
where regexp_like(studentid, '\D')
;
\D means non-digit character; if the studentid contains at least one such character, in any position, then it will appear in the output. Note that null will not be flagged out, assuming it may appear in the column; perhaps the column is primary key in which case it can't be null. But this would apply to other tables as well, where studentid may be null.
If you have a very large table, or if you must perform this check often, you may want a less simple, but better performing query. Then you would want to use standard string functions, like you were trying to. Something like this will work:
select studentid
from student
where translate(studentid, 'x0123456789', 'x') is not null
;
translate will translate x to itself, and all digits to null (that is, all digits will be removed). The x trick is needed because the last argument must not be null. If the translation doesn't remove all characters from the string, then the studentid will appear in the output, as required.
If you need to show exactly which characters are non-digits (although that should be obvious), you can add the result of translate to the select clause. Note though that if a student id has, for example, trailing spaces, that will not be evident either from looking at the student id or at the result of translate. You may want to add something like dump(studentid) to select; if you are not familiar with dump, you may want to read a bit about it - it is extremely useful in diagnosing such problems, and easy to learn.
Once you find and handle all the exceptions, you may want to add a constraint to the column, to require all student id's to consist entirely of digits. Then you won't have to put up with this kind of errors anymore.
If you want to allow numbers only, column datatype should have been NUMBER, not VARCHAR2.
[EDIT] That's wrong, though - see #mathguy's comment about it, saying that there are situations where values do consist of digits only, but - due to leading zeros - you can't use the NUMBER datatype.
A simple option is to use regexp_like and return rows that contain anything but digits:
SQL> with students (studentid) as
2 (select '12345' from dual union all
3 select 'ABC12' from dual union all
4 select '23x#2' from dual
5 )
6 select studentid
7 from students
8 where not regexp_like(studentid, '^\d+$');
STUDE
-----
ABC12
23x#2
SQL>
You could also use below solution taking advantage of translate function.
select studentid
from students
WHERE translate(studentid, '`0123456789', '`') IS NOT NULL
;
demo

Teradata Character Column with non alphabet values

I have a name column in Teradata that has customer full name all in one column. There are some names with -,_,.,/,#,! in between the name characters. I want to be able to pull records where there are names with these conditions. Is there a better option to pull records with the scenario below?
Currently, I am writing query like this
SELECT NAME FROM TABLESOURCE WHERE NAME LIKE ANY('%-%','%.%','%#%','%~%','%!%')
Thanks in advance.
I haven't tested this but I think you could test for equality when those characters are removed from the name using otranslate
select name
from tablesource
where name <> otranslate(name,'-.#~!','')

Is there a way to do a word search for values of one column in another column?

I have a database that contains columns that are text fields (strings, a few sentences long) and columns that are shorter strings (eg: college majors). Is there any way I can use the 'LIKE' function in SQL to search whether one of the values of the College Major column appears in another column?
I don't want to write out each of the college majors as a string since there are over 100.
Yes you can. Something like
where bigdatacolumn like '%' + computer_major + '%'
Since you said, other column contains few lines (Text column), you probably want to consider using Full Text Search instead of using LIKE operator
Use The ANY operator to compare in an array as shown in the below query.
select * from staff where department ILIKE ANY ( ARRAY['AUT%', '%COM%' ,'SP%' ] :: TEXT[] );

SQL - find rows with values that has 2 spaces or more in between

I have sql table with name and surname. Surname is in own column. The problem is with users with two surnames, because sometimes they add more than one space between surnames and then I have to find and fix them manualy.
How to find these surnames with more than one space in between?
If you want to find records which have more than one space then you can use the following trick:
SELECT surname
FROM yourTable
WHERE LENGTH(REPLACE(surname, ' ', '')) < LENGTH(surname) - 1
This query will detect two or more spaces in the surname column. If you want to also do an UPDATE this is possible, but it would be fairly database-specific, and you did not specify your database as of the time I wrote this answer.
First remove those extra spaces. Then add a constraint that makes sure it doesn't happen again:
alter table tablename add constraint surname_verify check (surname not like '% %')
(Or, even better, have a trigger making sure the surnames are properly spaced, cased etc.)
How to remove extra spaces? Depends on the dbms.
You can perhaps do something like:
update tablename set surname = replace(surname, ' ', ' ')
where surname like '% %'
The where clause isn't needed, but makes the transaction much smaller.
(Iterate to get rid of triple or more spaces.) Or use regexp_replace.
Even tidier:
select string = replace(replace(replace(' select single spaces',' ','<>'),'><',''),'<>',' ')
Output:
select single spaces

How do I return only duplicate values depending on column name?

Im using Access for sql. I know access is terrible but its the only available resource for database in my work.
I want to return duplicate values for, lets say, the middle name. I want to see all the rows with the same middle names on a certain month.
Here comes the tricky part, the duplicate values i want to look for is not in the table. I wanna see the duplicate values when you concatenate middle name AND last name. )and for some reason, Access dont like aliases)
You should group by on concatenated value:
select middle_name || last_name
from names_table
group by middle_name || last_name
having count(*) > 1