How to use LIKE with OR on Oracle?

Asked

Viewed 2,740 times

1

I have the following table:

CREATE TABLE DEPENDENTE
(
  IdDependente        number(6),
  Matricula           number(6),
  Nome                char(50) NOT NULL,
  dtNascimento        date,

  constraint pk_IdDependente PRIMARY KEY(IdDependente),
  constraint fk_DEPENDENTE foreign key(Matricula) references FUNCIONARIO(Matricula)
);

With the INSERTS :

INSERT INTO DEPENDENTE VALUES(1, 1010, 'Francisca', to_date('01/03/1978', 'dd/mm/yyyy'));
INSERT INTO DEPENDENTE VALUES(2, 2935, 'Joana', to_date('10/08/1984','dd/mm/yyyy'));
INSERT INTO DEPENDENTE VALUES(3, 6987, 'Hugo', to_date('01/09/2009','dd/mm/yyyy'));
INSERT INTO DEPENDENTE VALUES(4, 6987, 'Turine', to_date('10/06/2003','dd/mm/yyyy'));
INSERT INTO DEPENDENTE VALUES(5, 1234, 'Augusto', to_date('30/06/2005','dd/mm/yyyy'));

And with the following SELECT :

SELECT 'O dependente ' || InitCap(Nome) || ' é dependente do funcionário de código: ', Matricula FROM Dependente WHERE Nome LIKE ('T%') OR ('A%');

I want to select only dependents who have at the beginning of the name the letters T or A, but when using this SELECT above, it does not return me what I want. It returns nothing. How can I use correctly the operator with the LIKE ?

1 answer

3


You need to adjust your SQL to the conditions:

SELECT 'O dependente ' || InitCap(Nome) || ' é dependente do funcionário de código: ', Matricula 
FROM Dependente 
WHERE Nome LIKE 'T%' OR Nome LIKE 'A%';

Whenever there is a new condition, you repeat the column to be conditioned and put the operator. < coluna > < condicao > < referencia >

Example:

WHERE NOME = 'Joaquim' OR NOME <> 'Pedro'

See more: SQL AND, OR and NOT Operators

  • Thank you very much, I knew something was missing in the condition of LIKE, I just didn’t know where. + 1 and I will give also as a sure answer, just have to wait a while. Again, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.