How to replace the nth occurrence of a character in Excel
Formula (Excel and Google Sheets)
=SUBSTITUTE(A2,"-"," ",$E$1)Use SUBSTITUTE's fourth argument, the instance number: =SUBSTITUTE(A2,"-"," ",3) replaces only the third dash.
- Result on the example
- one result per row in
B2, filled down toB6 - Works in
- Every current version
- Google Sheets
- Same formula
See it work
The formula is already in B2, filled down to B6. Change any input and the result updates, or edit the formula to experiment.
How it works
Without its fourth argument, SUBSTITUTE replaces every match. With an instance number, it replaces only that one, counting from the left. Here the export joined the date and time with a dash, and replacing the third dash with a space gives a timestamp that converts to a real date and time.
=SUBSTITUTE(A2,"-"," ",$E$1)Result"2026-03-14 09:30"
A2"2026-03-14-09:30"The value in A2.
$E$13The value in E1.
SUBSTITUTE(A2,"-"," ",$E$1)"2026-03-14 09:30"SUBSTITUTE: Replaces matching text.- text
A2"2026-03-14-09:30" - old_text
"-" - new_text
" " - instance_num
$E$13
- text
In Google Sheets
The same formula works in Google Sheets: =SUBSTITUTE(A2,"-"," ",$E$1). We ran it in Google Sheets mode on the same data and it returns the same results.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =REPLACE(A2,FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),$E$1)),1," ") | 2026-03-14 09:30, 2026-03-15 14:05, 2026-04-01 08:00… | The long way: find the nth dash's position, then REPLACE that one character. Useful when you need the position anyway. |
Common mistakes
- The instance counts from the left only; there is no negative instance. For the last occurrence, use the count as the instance:
LEN(A2)-LEN(SUBSTITUTE(A2,"-","")). - If the text has fewer matches than the instance number, SUBSTITUTE returns it unchanged, with no error.
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Replace the nth occurrence of a character
B2, replace only the dash whose number is in E1 (the 3rd dash) with a space, and fill down to B6.Questions
How do I turn the result into a real date and time?
Wrap it in VALUE: =VALUE(SUBSTITUTE(A2,"-"," ",3)), then format the cell as a date and time.
How do I replace the last occurrence?
Use =SUBSTITUTE(A2,"-"," ",LEN(A2)-LEN(SUBSTITUTE(A2,"-",""))).
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.