# `Fun_FormulaReplace` (function)

> 公式计算解析函数


- **Type:** FUNCTION
- **Returns:** `longtext`
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sFormulaReplace` | `longtext` |
| 2 | IN | `replaceValue` | `varchar(255)` |
| 3 | IN | `toReplaceValue` | `varchar(255)` |

## Body

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

## Narrative

**Business context:** Token-substitution helper used by the formula-resolution family of procs (`Sp_System_ReplaceField*`) when expanding the variable placeholders inside a formula string (e.g. quote-price formulas, work-order calc formulas). One call replaces one `[name]` placeholder with its concrete value, plus a "concatenated trace" copy `[name]value` so downstream code can render the audit trail. Header comment is `公式计算解析函数` ("formula calc parsing function").

**What it does:** if `toReplaceValue` is purely numeric (regex `[^0-9.]=0`), it normalizes via `ROUND(CONVERT(...)+0,4)` to strip trailing zeros. Lowercases both keys. Returns `''` for empty input. Then `REPLACE`s the placeholder (in both `[name]` and full-width `【name】` forms) with `toReplaceValue` and with the concatenated `nametoReplaceValue` trace token.

**Invocation:** called from `Sp_System_ReplaceFieldNew`, `Sp_System_ReplaceField_Detail`, `Sp_System_ReplaceField_NEW`, `Sp_System_ReplaceField_NEW_ACT` — the formula-substitution upgrades shipped under `script/标版/upgrade/20220111公式解析实际详情` and `20220321计算获取公式解析`.
