Everyday SQL (12)

data:

code

A1

A2

A11

A22

A111

A222

A3333

A4444

 

We want to select a data set as follows.

A1

A2

A11

A22

A111

A222

 

Solution:

SELECT code

FROM data

WHERE code LIKE ‘A[0-9]’  — 1 digit

OR    code LIKE ‘A[1-9][0-9]’  — 2 digits

OR    code LIKE ‘A[1-9][0-9][0-9]’  — 3 digits

 

We can use len function as a filter

select * from data where code like ‘A[0-999]%’ and len(code)<=4

Be the first to comment

Leave a Reply

Your email address will not be published.


*