0

I have MySQL table wp422_posts and I need to manipulate one type of values in post_name column. Actually, I need to manipulate long value beginning with 4 digits (unique ID) and than text, I need to get the 4 digits and left them there. Could anybody help me, please? I'm new to MySQL. Other values - which do not start with 4 digits and dash has to stay same.

one example of value of post_name

  • old: "2147-sprava-uzivatelskych-uctu-databaze-oracle-v-prostredi-autocad-map-3d" desired
  • new one: "2147"

How to select correct values in column:

SELECT * FROM `wp422_posts` WHERE `post_name` LIKE "[0-9][0-9][0-9][0-9]%"

it returns empty result, but why?

How to get rid of not needed part?

Thanks relly much for your help.

Karolína Vyskočilová
  • 1,305
  • 1
  • 18
  • 29
  • You're trying to use a regular expression, but that's not what LIKE does. See https://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp – Hammerite Aug 17 '15 at 09:58

1 Answers1

0

If you are sure that starting part is always 4 digit number and that is only needed, then you can use this:-

SELECT LEFT(COL_NAME, 4) 
FROM `wp422_posts`

Then you can use update statement to update the table. Somthing like:-

UPDATE `wp422_posts`
SET COL_NAME = LEFT(COL_NAME, 4)

Hope this helps!!

Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40