Files
ai_trading_api/infra/postgres/sql/02_create_function.sql
2025-12-19 12:26:58 +08:00

20 lines
794 B
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
CREATE OR REPLACE FUNCTION update_data_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql VOLATILE;
-- 创建自动格式化小数的函数按需去除尾部多余0
CREATE OR REPLACE FUNCTION format_numeric_to_original(n NUMERIC)
RETURNS TEXT AS $$
BEGIN
-- 逻辑如果是整数小数部分全0返回整数文本否则返回去除尾部0的文本
IF n = TRUNC(n) THEN
RETURN TRUNC(n)::TEXT; -- 整数场景1.000000 → '1'
ELSE
RETURN TRIM(TRAILING '0' FROM TRIM(TRAILING '.' FROM n::TEXT)); -- 小数场景1.230000 → '1.23'1.002000 → '1.002'
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE; -- IMMUTABLE相同输入返回相同输出支持索引