How to use LIKE with OR in Oracle?

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 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 the dependents that have the letters T or A at the beginning of the name, but when using this SELECT above, it does not return what I want. It returns nothing. How can I correctly use the OR operator with LIKE ?

    
asked by anonymous 27.09.2017 / 17:55

1 answer

3

You need to adjust your SQL for 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 place the operator. < coluna > < condicao > < referencia >

Example:

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

See more: SQL AND, OR and NOT Operators

    
27.09.2017 / 17:58