comparing number to varchar - sql

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 {}.

Related

Why does code output ORA-00904 on code that I used a syntax verifier for?

So im trying to run a command in Oracel Apex to select a colum were primary key of tabel one and foreing key of tabel 2 are the same.
`select name from COMP_EMPL where COMP_EMPL.employee_id = MANAGER."employee_id";`
When i run it the error:
`ORA-00904: "MANAGER"."employee_id": invalid identifier apears`
The tabel names are corect and the column names are corect so its not a spelling isue.
The tabels are populated and coresponding values for the two keys exist.
Error
COMP_EMPL table
MANAGER table
Sorry the photots are as code but it would not let me post the question otherwise (smth about bad formating)
I have tried removing the "" from the column names but its solved nothing i also tried renaming the column but nothing changed same error with diferent name also the same error apears werever i use MANAGER."employee_id" so its not an isue with that specific comand.
First of all, code you posted is invalid. It is a join you need; you can't reference the MANAGER table in WHERE clause just because. Correct syntax is
select c."name"
from comp_empl c join manager m on m.employee_id = c."employee_id"
For example, with some sample data:
SQL> with
2 comp_empl ("employee_id", "name") as
3 (select 1, 'Little' from dual),
4 manager ("manager_id", employee_id) as
5 (select 100, 1 from dual)
Query you'd use:
6 select c."name"
7 from comp_empl c join manager m on m.employee_id = c."employee_id"
8 /
name
------
Little
SQL>
Note double quotes I used for identifiers. They are necessary because you chose to create tables using double quotes. It means that you have to enclose them into double quotes EVERY TIME you reference them. If you used mixed letter case, you'd have to match letter case as well.
Shortly: it is a bad idea to use double quotes when working with Oracle. By default, Oracle stores names (tables, procedures, columns, ...) in uppercase, but lets you reference them using any letter case you want unless you used double quotes.
If you look at e.g. MANAGER table, you'll see that manager_id is created in lower case (so you have to enclose it into double quotes and write lower case), while EMPLOYEE_ID is written in upper case (so you can reference it using upper case, or enclose them into double quotes but also with upper case).
On the other hand, all columns in COMP_EMPL table were created in lower case (which means with double quotes).
If I were you, I'd drop both tables and create new ones as e.g.
create table manager
(manager_id number,
employee_id number
);
Then you can use all this:
select * from MAnaGer where EMPLOYEE_id = 1
or
select EMPLOYEe_Id from manager where MANAGER_ID = 100
or ...
Using sample data from beginning of this post:
SQL> with
2 comp_empl (employee_ID, NAme) as
3 (select 1, 'Little' from dual),
4 manager (MANAGER_id, EmPlOyEe_ID) as
5 (select 100, 1 from dual)
6 select c.name
7 from comp_empl c join manager m on m.employee_id = c.employee_id;
NAME
------
Little
SQL>
The error appears because there is no "MANAGER" table in the FROM clause. If you reference a table in the where clause, it needs to exist in the FROM clause. In your case a simple join would do, there where clause is not needed. This statement does the same as what you're trying to achieve:
select name
from COMP_EMPL C
JOIN MANAGER M ON C.employee_id = M.employee_id

How to get length of string using Oracle query

In a table I have columns FIRST_NAME and LAST_NAME. I have to concatenate the two names and retrieve names containing more than 12 characters.
I tried the following query:
select *
from (select first_name, last_name
from customer as name
)
where length(NAME) = 12
Select *
from (
select first_name||last_name as name
from customer
)
where length(name)>12
You need only one SELECT
SELECT first_name || last_name AS name
FROM customer
WHERE LENGTH(first_name||last_name) > 12
Optionally TRIM name and last_name from spaces.
As an alternative, if you rewrite your question to check if the total length of first name and last name is more than 12, you probably get a different response from people with a more efficient code. It is not necessary to make Oracle actually concatenate the columns.
Programmers are too literal these days. :)
You are not concatenating the first name and last name.
Also no need to write an inline view, below is the query.
select FIRST_NAME || LAST_NAME as NAME
from CUSTOMER
where length(FIRST_NAME || LAST_NAME) > 12;

SQL checking the last letter in string / AKA LIKE strange behaviour

I know it was already answered but it doesn't work for me.
So quick introduce:
I have table called swimmers. I'll insert new record to it which fulfils all columns:
INSERT INTO swimmers(id, first_name, last_name, age, gender)
VALUES(9,'Maria','Spolsky',34,'Female');
Now I want to find records in swimmers table which first_name DOES NOT end with letter a and where gender is Female
So I wrote SQL query:
SELECT first_name, last_name
FROM swimmers
WHERE first_name NOT LIKE '%a' AND gender = 'Female'
But it does return Maria which we just added. It basically returns every female
I'm using ORACLE iSQL plus.
EDIT:
I tried to use substr(first_name, -1) = 'a' but it turned out that -1 is empty, because I use varchar(20) and names are usually smaller than 20 characters
EDIT2:
I tried to find issue in data type. I used char(20) for first_name.
I changed it into varchar2(20):
ALTER TABLE swimmers
MODIFY first_name varchar2(20);
but didn't solve the issue
EDIT 3:
Changing NOT LIKE to WHERE first_name LIKE '%a' returns no rows. I believe issues lies inside data type and empty spaces from the end of the first_name to the end of reserved space for string (names has ~10 characters and I use `varchar2(20))
Appearantly oracles is adding spaces behind the name and that's why '%a' doesn't work, either change your column definition to a varchar (so oracle doesn't add the extra spaces) or trim the spaces out of your name like this:
SELECT first_name, last_name
FROM swimmers
WHERE NOT trim(first_name) LIKE '%a' AND gender = 'Female';

How Do I SELECT when needing to use LIKE and change column Name

Here is the problem I am working on:
Select all the Oracle database employees whose last names end with “s” Change the
heading of the column to read Possible Candidates.
I tried
SELECT last_name AS possible_candidates FROM * WHERE last_name LIKE '%s';
That returned the error :
ORA-00903: invalid table name
Unless I am reading the question wrong how do I check the entire database for something like this?
To answer the question which is asking for employee names not just last names you would select all employees with a last name that ends in S.
SELECT *
FROM employees
WHERE last_name LIKE '%s'
If the table has more than a first_name and last_name column you can do
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%s'
Now to combine your two parts (Select employees and rename column)
Run one of the two queries above but add an AS statement as show in the example below.
SELECT *
AS Possible_Candidates
FROM employees
WHERE last_name LIKE '%s'
Based on feedback: this appears to be your answer...
Is there a table called employees? if that I would interpret the question as from the employees table. Otherwise, you could select table_name from All_TAB_COLS where column_name like = 'LAST_NAME' and then build a union from the resulting tables all of which have a field called 'LAST_NAME'

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