# `Stuff` (function)

> 使用字符串替换从指定位置开始指定长度的字符，返回新字符串


- **Type:** FUNCTION
- **Returns:** `varchar(8000)`
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `str` | `varchar(8000)` |
| 2 | IN | `startIndex` | `int` |
| 3 | IN | `length` | `int` |
| 4 | IN | `Newstr` | `varchar(8000)` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE FUNCTION `Stuff`'`._

## Narrative

**Business context:** 使用字符串替换从指定位置开始指定长度的字符，返回新字符串 — SQL-Server `STUFF` polyfill. Removes a `length`-character window starting at `startIndex` from `str` and splices `Newstr` in its place. Used by the formula-replacement engine to substitute placeholders inside cost-formula strings at known offsets.

**What it does:** returns `CONCAT(LEFT(str, startIndex-1), Newstr, RIGHT(str, ...))`. The right-hand slice uses `LOCATE(SUBSTR(str, startIndex, length), str)` instead of the simpler `startIndex + length`, so when the deleted substring also occurs *earlier* in `str` the function would slice from the earlier occurrence (subtle bug — only safe when the substring is unique or the first occurrence is exactly at `startIndex`).

**Invocation:** referenced by the auto-send-check-message + formula-replace family: `Sp_System_AutoSendCheckMsg`, `Sp_System_ReplaceField`, `Sp_System_ReplaceFieldNew`, `Sp_System_ReplaceField_Detail`, `Sp_System_ReplaceField_NEW`, `Sp_System_ReplaceField_NEW_ACT`. Also referenced in xly-src upgrade scripts under `script/标版/upgrade/20220111公式解析实际详情/` and `20220321计算获取公式解析/`. No form-master binding — strictly an internal string helper.
