开发者

Updating Varchar Field So That Number Within Field is to 3 Decimal Places

This is a fairly odd question... I believe it would involve some sort of regex with MySQL.

Say 开发者_运维百科I have a query on a table as such below:

    >select description from foo;
 ----------------------------------
 |   description                  |
 ----------------------------------
  Kitten, 3 in., Lightsaber
  Mouse, 5.5 in., Luke Skywalker
  Dog, 9.000 in., Death Star

Is there a way to update foo such that the "inches" (in.) portions are to three decimal places.

i.e. After the update:

    >select description from foo;
 -----------------------------------
 |   description                    |
 -----------------------------------
  Kitten, 3.000 in., Lightsaber
  Mouse, 5.500 in., Luke Skywalker
  Dog, 9.000 in., Death Star

Any advice/guidance is greatly appreciated! Thanks.


According to the documentation, that's not possible in a SELECT command.

REGEXP only tells you if it's a match or not; it does not provide match indexes or substitution.

You should export the data to a flat text file, treat it with your favorite Regex enable text editor and re-apply the data to a brand new data structure. Seriously, you have to change your DB design.

Update

After further investigation, it seems like my answer should be: it's not possible out of the box.

Check this topic: How to do a regular expression replace in MySQL? You can do that via SQL through UDF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜