Jump to content

Require SQL to strip data from a field


warrenk

Recommended Posts

http://www.website.com/product-xx11-p-16191.html

http://www.website.com/product-xx22-p-16192.html

(etc)

 

I am trying to strip out the number ie. 16191, 16192, etc) into a field.  The number is always just before the '.html' and right after '-p-'.  Any suggestions?  If it requires more then one SQL...not a problem.

 

Appreciate any help!

Warren

 

 

 

 

Link to comment
Share on other sites

If the url is always in the pattern you posted you should be able to use the following.

SELECT
SUBSTRING(SUBSTRING_INDEX(columnname, '.', 3), LOCATE('-p-', columnname)+3) AS num
FROM
tablename

 

If the '-p-' is always the same number of characters from the beginning of the url string you can hardcode the second argument.

 

Using PHP a better solution may be

<?php
preg_match('/-p-(\d+)\.html/i', $row['columname'], $matches);
echo $matches[1];
?>

 

EDIT: query edit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.