SAPFree generatorPDF, Excel, Word exportSAP S/4HANAChemical Manufacturing

SAP Batch Expiry Stock SQL Query Generator

Chemical and specialty materials carry shelf lives, and expired batches turn into write-offs or quality incidents. This generator builds a HANA SQL query that joins the batch stock table MCHB to the batch master MCH1 and lists what is about to expire, with the stock split by status.

Fill in

Use your database schema, often SAPHANADB or SAPABAP1.

Generated code
Download:

Batches expiring within the window

SELECT b.MATNR,
       b.WERKS,
       b.LGORT,
       b.CHARG,
       m.LICHA AS VENDOR_BATCH,
       m.HSDAT AS MANUFACTURE_DATE,
       m.VFDAT AS EXPIRY_DATE,
       b.CLABS AS UNRESTRICTED,
       b.CINSM AS QUALITY_INSP,
       b.CSPEM AS BLOCKED
FROM SAPHANADB.MCHB AS b
INNER JOIN SAPHANADB.MCH1 AS m
        ON m.MANDT = b.MANDT
       AND m.MATNR = b.MATNR
       AND m.CHARG = b.CHARG
WHERE b.MANDT = '100'
  AND b.WERKS = '1000'
  AND m.VFDAT <> '00000000'
  AND m.VFDAT <= TO_VARCHAR(ADD_DAYS(CURRENT_DATE, 90), 'YYYYMMDD')
  AND (b.CLABS + b.CINSM + b.CSPEM) > 0
ORDER BY m.VFDAT, b.MATNR, b.CHARG;

Total quantity at risk by material

SELECT b.MATNR,
       COUNT(DISTINCT b.CHARG) AS BATCH_COUNT,
       SUM(b.CLABS) AS UNRESTRICTED_QTY
FROM SAPHANADB.MCHB AS b
INNER JOIN SAPHANADB.MCH1 AS m
        ON m.MANDT = b.MANDT
       AND m.MATNR = b.MATNR
       AND m.CHARG = b.CHARG
WHERE b.MANDT = '100'
  AND b.WERKS = '1000'
  AND m.VFDAT <> '00000000'
  AND m.VFDAT <= TO_VARCHAR(ADD_DAYS(CURRENT_DATE, 90), 'YYYYMMDD')
GROUP BY b.MATNR
ORDER BY UNRESTRICTED_QTY DESC;

Read-only SQL for a replica or reporting database, not for production dialog use. Confirm table and column names on your release, check whether batches are managed at plant level (MCHA) in your configuration, and note that quantities are in base unit of measure. Date columns are character fields in YYYYMMDD format.

Expiry monitoring without custom reports

Ask how a read-only assistant over your SAP batch data could flag expiring stock and show the query behind each answer.

Optional. The tool and every export work without this. No spam.

How to use the generator

Enter the client, plant, schema and the number of days you want to look ahead, then copy the SQL into your SQL console or reporting tool against a replica or reporting schema. The first query lists each batch with its manufacture date, expiry date and stock in unrestricted, quality inspection and blocked status. The second summarizes the unrestricted quantity per material so planners can prioritize what to consume, sell or retest first.

  • Run against a read-only copy or replicated schema
  • Change the window to 30, 60 or 180 days for different purposes
  • Join MAKT afterward if you want material descriptions

How the tables fit together

MCHB holds batch stock per material, plant, storage location and batch, split into unrestricted use (CLABS), quality inspection (CINSM) and blocked (CSPEM) quantities. MCH1 is the cross-plant batch master that holds the shelf life expiration date VFDAT, the date of manufacture HSDAT and the vendor batch LICHA. The join uses client, material and batch. A zero date in VFDAT means no expiry is maintained, which is why the query excludes it.

Common mistakes

Frequent problems are querying without the client, comparing dates with the wrong format, and forgetting that expiry can be extended by a retest or recalculation. Stock in quality inspection or blocked status may still be relevant for write-off provisioning, so decide which quantities your report should count. For special stock such as consignment or project stock, check the separate stock tables and add them if required.

  • Always filter by client
  • Decide whether blocked and quality stock count as at risk
  • Check special stock tables if you use consignment or project stock

Frequently Asked Questions

Where is the SAP batch expiry date stored?

In the batch master table MCH1 as VFDAT, the shelf life expiration date, with the manufacture date in HSDAT. In plant-level batch setups the data may also be held in MCHA. The stock itself sits in MCHB.

What do CLABS, CINSM and CSPEM mean?

They are the unrestricted-use, quality inspection and blocked stock quantities of a batch in MCHB. Together with restricted-use and in-transfer fields, they make up the total batch stock for the location.

Will this work outside HANA?

The functions ADD_DAYS and CURRENT_DATE are HANA syntax. For another database, replace the date arithmetic with its equivalent and keep the YYYYMMDD string comparison.

Why is a batch missing from the result?

Likely causes are no expiry date maintained, zero stock in all status fields, a different plant, or a batch managed on a different level. Check the batch master and the stock quantity for the material first.

For a conversational view of expiring stock without building SQL for each question, Netray can set up a read-only assistant over your SAP data that shows the query it ran.