Home > Software engineering >  Sql Query syntax to show values by a given number of starting letters
Sql Query syntax to show values by a given number of starting letters

Time:05-09

Learning sql, supposedly from a course but its incredibly unreliable. Anyway, as per title sorting out values by letters, starting letter in this case, what would be the correct syntax to write a code such as the following that actually work. Works with a single Like 'Letter%' fails with multiple.

Ty for your time.

SELECT * FROM Table WHERE Column LIKE 'R%' (OR 'D%' OR any other letter) ;

Ps: Using sql shell/psql/pgadmin to write queries.

CodePudding user response:

You can try this:

select *
from table
where column like 'R%'
   or column like 'D%'
   or column like 'X%'

Example: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=99feae2198aed32a240169612123bb69

Example table:

create table testing (firstname varchar(100));

insert all
  into testing (firstname) values ('Mike')
  into testing (firstname) values ('John')
  into testing (firstname) values ('Matthew')
  into testing (firstname) values ('Randy')
  into testing (firstname) values ('Toledo')
  into testing (firstname) values ('Tallon')
select 1 from dual;

This query:

select * from testing
where firstname like 'M%' or firstname like 'J%'

Will return Mike, John and Matthew.

  • Related