# Implementation guide for digital medicines

This guidance is under active development by NHS Digital and content may be added or updated on a regular basis.

## Detailed Logic for Step 2 - Calculate the required quantity of each VMP to fulfil the requested dose

### Conversion between scaler units of measure, e.g. gram to milligram

A function is required to convert a VTM ingredient strength into the same units as the requested dose quantity.

For example, if a required dose quantity is `12.5 milligram`, but a VMP for that drug is expressed with a strength in micrograms e.g. `500 microgram`, then that strength needs to be expressed in milligrams before the mathematical function can be executed.

Thus `500 microgram` would be converted into `0.5 milligram`.

For example;

```///
// Convert 500 micrograms into milligrams
// FNC_CONVERT_UNITS(vpi.strnt_nmrtr_val, vpi.strnt_nmrtr_uomcd, dose_uom_cd);
///

SELECT FNC_CONVERT_UNITS(500, 258685003, 258684004);

// returns `0.5`.

```

Within the dm+d, units of mass have the greatest range; kilogram, gram, milligram, microgram and nanogram.

Due to this range, the data type used within SQL must be a `DECIMAL(30,12)`.

Conversion is required for the following units of measure.

Category Units SNOMED Code Scaler Conversion
Mass kilogram 258683005 10^3
gram 258682000 1
milligram 258684004 10^-3
microgram 258685003 10^-6
nanogram 258686002 10^-9
Volume litre 258770004 1
millilitre 258773002 10^-3
microlitre 258774008 10^-6
nanolitre 282113003 10^-9
Length metre 258669008 1
centimetre 258672001 10^-2
millimetre 258673006 10^-3

### Function for quantity

A suitable SQL function to calculate the quantity of a given VMP to fulfil the requested dose quantity would be as follows.

```FUNCTION FNC_CALC_QTY( doseQty DECIMAL(9,3)
, numerator DECIMAL(30,12)
, denominator DECIMAL(9,3)
, unitDoseFormStrength DECIMAL(9,3)
)
RETURNS DECIMAL(30,12)
BEGIN

IF denominator = 0
THEN SET denominator = 1;
END IF;

IF unitDoseFormStrength = 0
THEN RETURN doseQty / ( numerator / denominator );
END IF;

RETURN ( doseQty / ( numerator / denominator ) ) / unitDoseFormStrength;

END

```

A description of each variable used in `FNC_CALC_QTY` is contained below.

Variable Description
`doseQty` the required dose quantity - e.g. 12.5
`numerator` the VMP strength numerator, but has to be expressed in the same units as the requested dose, e.g. both in milligrams
`denominator` the VMP strength denominator which may be `0` / `NULL` for some VMPs, so use a default value of 1
`unitDoseFormStrength` the VMP unit dose form strength value, which may be `0` / `NULL`