The OSGB national grid uses nested 5x5 squares and two sets of digits to provide a location (see https://en.wikipedia.org/wiki/Ordnance_Survey_National_Grid#Grid_digits):
For example, the grid reference of the 100 m square containing the summit of
Ben Nevis is NN 166 712. (Grid references may be written with or without spaces;
e.g., also NN166712.) NN has an easting of 200 km and northing of 700 km, so the
OSGB36 National Grid location for Ben Nevis is at 216600, 771200.
The following link has an example of code to convert from e.g. NN166712 to 216600, 771200 https://www.movable-type.co.uk/scripts/latlong-os-gridref.html
create table letters (id serial primary key, letters varchar(2),
x int, y int);
-- add the valid letters for the main squares
insert into letters (letters) values (
unnest(array['SV', 'SQ', 'SL', 'SF', 'SA', 'NV', 'NQ', 'NL', 'NF',
'NA', 'HV', 'HQ', 'HL', 'SW', 'SR', 'SM', 'SG', 'SB',
'NW', 'NR', 'NM', 'NG', 'NB', 'HW', 'HR', 'HM', 'SX',
'SS', 'SN', 'SH', 'SC', 'NX', 'NS', 'NN', 'NH', 'NC',
'HX', 'HS', 'HN', 'SY', 'ST', 'SO', 'SJ', 'SD', 'NY',
'NT', 'NO', 'NJ', 'ND', 'HY', 'HT', 'HO', 'SZ', 'SU',
'SP', 'SK', 'SE', 'NZ', 'NU', 'NP', 'NK', 'NE', 'HZ',
'HU', 'HP', 'TV', 'TQ', 'TL', 'TF', 'TA', 'OV', 'OQ',
'OL', 'OF', 'OA', 'JV', 'JQ', 'JL', 'TW', 'TR', 'TM',
'TG', 'TB', 'OW', 'OR', 'OM', 'OG', 'OB', 'JW', 'JR',
'JM' ]));
-- set the basic x and y values
update letters set x = 8, y = 8 where left(letters,1) = 'H';
update letters set x = 9, y = 9 where left(letters,1) = 'J';
update letters set x = 13, y = 13 where left(letters,1) = 'N';
update letters set x = 14, y = 14 where left(letters,1) = 'O';
update letters set x = 18, y = 18 where left(letters,1) = 'S';
update letters set x = 19, y = 19 where left(letters,1) = 'T';
-- ToDo - multiply x, y, value by 100000 and add offset
-- Grid:
-- ABCDE
-- FGHJK -- No I
-- LMNOP
-- QRSTU
-- VWXYZ
-- A|F|L|Q|V -- x offset 0, y offset 0
-- B|G|M|R|W -- offset 20000, y offset 0
Then set up a query to split the NN166712 into 'NN', 166::integer, 712::integer and generate your grid value.