简体   繁体   中英

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.

ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM