Find column values where first letter is upper case - sql

I have a table EMP_INFO with EID, ENAME, GENDER. My objective is to display only those ENAME values where the first letter is Capital or uppercase.
Table like:
EID ENAME GENDER
001 Samuel M
002 john M
003 susan F
004 CALEB M
Desired output like:
EID ENAME
001 Samuel
004 CALEB
I have tried:
SELECT EID, ENAME
FROM EMP_INFO
WHERE ENAME like '[A-Z]%';
But this is just giving a blank output. No errors, no warnings but no output as well. Also I am using oracle sql developer.

Oracle does not support wildcards in the LIKE pattern. You can use regular expressions instead:
select EID , ENAME
from EMP_INFO
where regexp_like(ENAME, '^[A-Z]');
Alternatively, you could just compare the first character:
where substr(ENAME, 1, 1) BETWEEN 'A' AND 'Z'
Here is a working example of this version.
By default, Oracle is case-sensitive, so these should work on most Oracle systems.

We have a well-known function called initcap to be considerable :
SELECT EID, ENAME
FROM EMP_INFO
WHERE substr(ENAME,1,1) = substr(initcap(ENAME),1,1);
or alternatively use :
SELECT EID, ENAME
FROM EMP_INFO
WHERE ENAME between chr(65) and chr(92);
SQL Fiddle Demo

You might try something like the following (assuming you've not done anything to make your SQL queries case-insensitive):
SELECT eid, ename
FROM emp_info
WHERE ename >= 'A'
AND ename < CHR(ASCII('Z')+1);
This will ensure that the first character of ename falls between A and Z inclusive. The value of CHR(ASCII('Z')+1) is [ but that's not terribly important - I think it's clearer to use the functions in this case than the "magic character".
EDIT: The reason this works is that lower-case characters, as a group, appear after upper-case characters in many character sets*, so as long as a value of ename is between A and Z inclusive, according to the typical string comparison, it will start with an upper-case character.
*For example, here is the Unicode character table and here is the ASCII character table. I suspect that this solution may not work with EBCDIC character sets but I don't have a server handy on which I can confirm that suspicion.

Related

Selecting words that end in 'n' in oracle sqlplus

Select first names and last names of the table teachers where first names end in 'n'.
I wrote:
select first_name, last_name
from teachers
where type='teacher'
and last_name like '%n';
Unfortunately it selects no rows. It should select at least two.
The problem is with " last_name like '%n' " and I can't find a solution. Does anyone have any idea how i can solve this?Screenshot
The thing is, in your example there are no first_name columns that end with n. Yep.
Why? The the column type of first_name is CHAR instead of VARCHAR2, so Oracle fills up the column with spaces when each first name is shorter than 10 characters.
Solutions?
Use VARCHAR2 instead of CHAR as in:
create table teacher (
last_name varchar2(10), -- VARCHAR2 now!
first_name varchar2(10), -- VARCHAR2 now!
type varchar2(10)
);
With this structure your query will run well.
Alternatively, if you don't want to change the table structure, you can TRIM() the column, as in:
select first_name, last_name
from teachers
where type='teacher'
and trim(first_name) like '%n'; -- use TRIM here!
Try using. This will eliminate the chances of leftout of Case sensitive Data.
select first_name, last_name from teachers where lower(type)='teacher' and lower(first_name ) like '%n';
as per the screenshot the first_name, last_name columns have datatype as CHAR.
the thing about CHAR is that is appends spaces to reach its full length.
Example: for first_name CHAR(10) if u store 'Andra' it will be stored as 'Andra '
so use the trim function:
select first_name, last_name
from teachers
where type='teacher'
and trim(first_name) like '%n';
alternatively use the VARCHAR2 datatype instead of CHAR.
it does not append spaces at the end to reach its full length.
As you were told, your query should work. Problem is probably trivial (does case matters? Are there really lower case "n" letters in those names?).
Meanwhile, a few options for you:
SQL> select ename from emp
2 where ename like '%N';
ENAME
----------
ALLEN
MARTIN
SQL> select ename from emp
2 where substr(ename, -1) = 'N';
ENAME
----------
ALLEN
MARTIN
SQL> select ename from emp
2 where regexp_like(ename, 'N$');
ENAME
----------
ALLEN
MARTIN
SQL>
[EDIT: CHAR datatype issue]
SQL> create table empc (ename char(10));
Table created.
SQL> insert into empc select ename from emp;
12 rows created.
SQL> select ename from empc where ename like '%N';
no rows selected
SQL> select ename from empc where trim(ename) like '%N';
ENAME
----------
ALLEN
MARTIN
SQL>
If you have char data type then you should try trimming of space chars from your first_name values
select first_name, last_name
from teachers
where type='teacher'
and trim(first_name) like '%n';
anyway in your screeshot you have not teacher with firt_name endig with n ..try
select first_name, last_name
from teachers
where trim(first_name) like '%n';
You could try something like
SELECT *
FROM teachers a
WHERE TRIM (LOWER (a.first_name)) LIKE '%n';

Is there way to make SQL+ accept a parameter that isn't a number?

I'm stuck on this Homework problem for Oracle Plus, so any pointers in the right direction would be great. I need to...
"1.Create a script that will take an parameter of division(DIV) with script execution and generate employee report for all employee in this division."
Here's the layout of the relevant tables
Divisions
-Division_ID, Name
Employees2, Employee_ID, Division_ID, Job_ID, First_Name, Last_Name, Salary
So far I have
SET DEFINE '#'
SELECT Last_Name || ‘,’ || first_name AS Name, Divisions.Name
FROM Employees INNER JOIN Divisions
ON Employees2.Division_ID = Divisions.Divisions_ID
WHERE Division_ID = #v_Division_ID
It asks me for the division_id input but when i put it in it doesn't work. Now being that the Division_ID column isn't a number but rather an abbreviation like "SAL" for sales It seems that the error is because oracle plus wont take letters as input but rather only numbers? Is there a way to make this script so it accepts string/data that isn't numbers but rather something like "sal"?
When the substitution variable is being compared to a VARCHAR2, you want to enter the value you want to supply, e.g. 'SAL', with single quotation marks around it.
Here is an example from the SCOTT sample schema:
SCOTT#dev> SET DEFINE '#'
SCOTT#dev>
SCOTT#dev>
SCOTT#dev> SELECT e.ename employee_name
2 FROM emp e
3 INNER JOIN dept d
4 ON e.deptno = d.deptno
5 WHERE d.dname = #v_dname
6 /
Enter value for v_dname: 'SALES'
old 5: WHERE d.dname = #v_dname
new 5: WHERE d.dname = 'SALES'
EMPLOYEE_N
==========
ALLEN
WARD
MARTIN
BLAKE
TURNER
JAMES
6 rows selected.
Thus for you, you just need to enter 'SAL' for your #v_Division_ID.

comparing number to varchar

I have a column called Dept which is a number and I have another column called manager which is a varchar. The manager column has a list of numbers in varchar format (1,2,3,4), but also has stores the symbol '%'. My problem is in my query I compare dept to manager
Where dept = [value in manager]
If I am comparing dept to a manager value which is a varchar but contains a number character the query works, but when I compare the dept to the manager variable and it contains the '%' it does not work anymore.
So to make it clearer I will show some examples
Where dept = '1' -OK
Where dept = '2' -OK
Where dept = '%' -NOT OK
I tried to_char(dept)='%' with no luck. I am not sure what else to do?
With Where dept = '%' you are trying to find rows where dept is equal to one character: '%'. You should use Pattern-matching Conditions and it looks like this Where dept like '%'
As % is a wildcard-character you must quote it: Here is a list of all special-characters recognized by oracle and how they can be quoted with braces {}.

How to find the count of empty space expressions in column values?

if you have
e_id ename
1 hansen sahul
2 dennis richard rathore
how to find the ename count of e_id 1 as ? 2
how to find the ename count of e_id 2 as ? 3
Unfortunately, the string manipulation functions vary between databases, so I can give SQL Server dialect. If you're using a different database, you'll have to find the replacements for LEN and REPLACE:
declare #Names table (e_id int,ename varchar(3000))
insert into #Names (e_id, ename) values
(1,'hansen sahul'),
(2,'dennis richard rathore')
select e_id,LEN(ename) - LEN(REPLACE(ename,' ','')) + 1 as namecount
from #Names
Result:
e_id namecount
----------- -----------
1 2
2 3
This works be calculating the difference in length between the plain string, and the same string where spaces have been removed. This is logically equivalent to the number of spaces in the string. We then add 1 to match your desired result (the number of names, rather than the number of spaces).
(The table setup is also dialect, but just there to help create a working script)

how to display employee names starting with a and then b in sql

i want to display the employee names which having names start with a and b ,it should be like list will display employees with 'a' as a first letter and then the 'b' as a first letter...
so any body tell me what is the command to display these...
To get employee names starting with A or B listed in order...
select employee_name
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name
If you are using Microsoft SQL Server you could use
....
where employee_name LIKE '[A-B]%'
order by employee_name
This is not standard SQL though it just gets translated to the following which is.
WHERE employee_name >= 'A'
AND employee_name < 'C'
For all variants you would need to consider whether you want to include accented variants such as Á and test whether the queries above do what you want with these on your RDBMS and collation options.
select columns
from table
where (
column like 'a%'
or column like 'b%' )
order by column asc
Regular expressions work well if needing to find a range of starting characters. The following finds all employee names starting with A, B, C or D and adds the “UPPER” call in case a name is in the database with a starting lowercase letter. My query works in Oracle (I did not test other DB's). The following would return for example:
Adams
adams
Dean
dean
This query also ignores case in the ORDER BY via the "lower" call:
SELECT employee_name
FROM employees
WHERE REGEXP_LIKE(UPPER(TRIM(employee_name)), '^[A-D]')
ORDER BY lower(employee_name)
select employee_name
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name
What cfengineers said, except it sounds like you will want to sort it as well.
select columns
from table
where (
column like 'a%'
or column like 'b%' )
order by column
Perhaps it would be a good idea for you to check out some tutorials on SQL, it's pretty interesting.
If you're asking about alphabetical order the syntax is:
SELECT * FROM table ORDER BY column
the best example I can give without knowing your table and field names:
SELECT * FROM employees ORDER BY name
select name_last, name_first
from employees
where name_last like 'A%' or name_last like 'B%'
order by name_last, name_first asc
select *
from stores
where name like 'a%' or
name like 'b%'
order by name
Here what I understood from the question is starting with "a " and then "b"
ex:
abhay
abhishek
abhinav
So there should be two conditions and both should be true means you cant use "OR" operator
Ordered by is not not compulsory but its good if you use.
Select e_name from emp
where e_name like 'a%' AND e_name like '_b%'
Ordered by e_name
From A to Z:
select employee_name from employees ORDER BY employee_name ;
From Z to A:
select employee_name from employees ORDER BY employee_name desc ;
Oracle:
Just felt to do it in different way. Disadvantage: It doesn't perform full index scan. But still gives the result and can use this in substring.
select employee_name
from employees
where lpad(employee_name,1) ='A'
OR lpad(employee_name,1) = 'B'
order by employee_name
We can use LEFT in SQL Server instead of lpad . Still suggest not a good idea to use this method.
We can also use REGEXP
select employee_name
from employees
where employee_name REGEXP '[ab].*'
order by employee_name