How to extract numbers from text in a cell
✓ Verified in LibreOffice 25.8.7.3 Google Sheets returned something else (2026-08-30)Pull just the digits out of a cell that mixes text and numbers, like an order code or address.
The formula
| App | Formula | Notes |
|---|---|---|
| Excel (desktop) | =TEXTJOIN("",TRUE,IFERROR(MID(A2,SEQUENCE(LEN(A2)),1)*1,"")) | Excel 365/2021 (needs SEQUENCE + TEXTJOIN). |
| Google Sheets | =REGEXREPLACE(A2,"[^0-9]","") | REGEXREPLACE is Sheets-native and the cleanest way here. Skip the MID+SEQUENCE form in Sheets: executed 2026-08-30 it came back blank against 409215 in LibreOffice, because Google Sheets does no implicit array evaluation inside a scalar function’s arguments. =TEXTJOIN("",TRUE,ARRAYFORMULA(IFERROR(MID(A2,SEQUENCE(LEN(A2)),1)*1,""))) is the documented wrap of it. Both are documented Sheets syntax, carrying no executed result of their own. |
| LibreOffice Calc | =TEXTJOIN("",TRUE,IFERROR(MID(A2,SEQUENCE(LEN(A2)),1)*1,"")) | Works in LibreOffice 25.8+ (SEQUENCE arrived in 24.8). |
How it works
SEQUENCE(LEN(A2)) generates one index per character, MID splits the text into single characters, multiplying by 1 turns digits into numbers and everything else into an error, IFERROR blanks the errors, and TEXTJOIN glues the surviving digits back together. "Order #4092 (qty 15)" becomes "409215". The result is text; wrap the whole thing in VALUE() if you need a real number. In Google Sheets, REGEXREPLACE(A2,"[^0-9]","") does the same job in one step.
The Google Sheets alternative
Google Sheets needs different syntax for this task. The formula below is Sheets-specific — it was executed in Google Sheets (2026-08-30) against the same sample data as the worked example, and the value beside it is what Google returned. The LibreOffice column reads n/a because the formula is outside LibreOffice’s dialect, so there is nothing of ours to report there.
| Formula | What it does | Returned by LibreOffice 25.8.7.3 | Returned by Google Sheets (executed 2026-08-30) |
|---|---|---|---|
=REGEXREPLACE(A2,"[^0-9]","") | REGEXREPLACE strips every non-digit — Sheets-native, no per-character array | n/a (Sheets-only formula) | 409215 Google Sheets alternative (executed 2026-08-30) |
Verified, not just documented
We ran =TEXTJOIN("",TRUE,IFERROR(MID(A2,SEQUENCE(LEN(A2)),1)*1,"")) in LibreOffice 25.8.7.3 (headless, with forced recalculation) and it returned 409215 — exactly the expected result. We then ran the same formulas in Google Sheets, executed 2026-08-30: a formula-only workbook goes into Google Drive, which converts it to a Sheet and recalculates every formula with Google’s own engine, and comes back out as .xlsx carrying the values Google computed. For the worked example Google Sheets returned None, which is not what LibreOffice returned (409215) — both values are shown as each engine produced them, and the disagreement itself is the finding. A further 1 row is a Google Sheets alternative: Sheets-specific syntax, executed in Google Sheets only, so the LibreOffice column reads n/a for it. Both engines’ numbers on this page are executed results. The Excel formula follows Microsoft’s official documented syntax — we do not run desktop Excel.
Functions used
TEXTJOIN · IFERROR · MID · SEQUENCE · LEN · REGEXREPLACE — see full Excel, Google Sheets & LibreOffice compatibility for each.
Related recipes
- How to reverse a text string
- How to capitalize only the first letter (sentence case)
- How to extract the domain from an email address
- How to extract the last name from a full name
- How to remove text before a character