【问题标题】:Add a decimal after third character for every record with more than 3 characters对于每条超过 3 个字符的记录,在第三个字符后添加一个小数
【发布时间】:2018-07-24 09:37:03
【问题描述】:

我有一列需要在第三个字符之后插入一个小数,对于 char_length 大于 4 的每一行

例如。当前

Name
_______
1234
123
12345

想要的输出

Name
_______
123.4
123
123.45

到目前为止,我只能选择要更新的记录,但不确定如何插入到特定索引中。

SELECT *
FROM t1
WHERE CHAR_LENGTH(name) > 4

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    你可以使用insert()函数:

    select insert(name, 4, 0, '.')
    from t1
    where char_length(name) > 4;
    

    请注意,代码应该在没有where 子句的情况下执行您想要的操作。

    这会在字符串的第四个位置插入一个小数点。

    【讨论】:

    • 完美!我仍然不清楚 (name, 4, 0, '.') 如何指定正确的位置。但它确实有效。
    猜你喜欢
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    相关资源
    最近更新 更多