CREATE TABLE CODES(analysis_date Date, code Varchar(50));
INSERT INTO CODES VALUES('2021-01-01','PROD_OP21_115');
INSERT INTO CODES VALUES('2021-02-06','PROD_TR21_009R');
INSERT INTO CODES VALUES('2021-03-14','PROD_TR21_002');
INSERT INTO CODES VALUES('2021-05-01','PROD_OP21_050R');
INSERT INTO CODES VALUES('2020-06-04','PROD_OP20_450');
I have a column that stores a string, for example PROD_OP21_182
. I am developing a query that returns all codes that are correctly entered (meet all conditions).
- It always starts with
PROD_
. - After
PROD_
, there should be one of these acronyms:OP
,UG
orTR
. - 21 means the last two digits of the year given by another column (analysis date).
- A last
_
and three numbers. - The code can end with an R (it is optional). Example:
PROD_OP21_182R
. - Uppercase always.
This is my attempt:
select CODE
from CODES
where SUBSTRING(CODE, 1, 4) = 'PROD'
and SUBSTRING(CODE, 5, 1) = '_'
and SUBSTRING(CODE, 6, 2) in ('OP','UG','TR')
and RIGHT(YEAR(analysis_date), 2) = SUBSTRING(CODE, 8, 2)
and SUBSTRING(CODE, 10, 1) = '_'
and (
(len(CODE) = 13)
or
(len(CODE) = 14) and CODE like '%R')
It works partially. I still need to check that the three characters in SUBSTRING(dispatch_number, 11, 3)
are numbers, and that the rule of capital letters is fulfilled (simply, there cannot be lowercase in the code).