Forum Moderators: coopster
Example if I have a column called DESCRIPTION and I issue the following command.
select DESCRIPTION from DB
And the following is returned
"I want to select text up to this point. But not this"
What command would I issue to retreive the text up to the first period. So the following is returned.
"I want to select text up to this point."
Thanks in advance for any help.
Most of the SQL fanciness comes in constructing search queries (which can have some pretty intense performance ramifications themselves).
Just get the whole thing, and then use PHP to REGEX it with preg_match or preg_replace.
Look here [dev.mysql.com] to see if you can find something.
Good luck. Let us know if you do.
SELECT SUBSTRING('I want to select text up to this point. But not this' FROM 1 FOR POSITION('.' IN 'I want to select text up to this point. But not this')) AS mySubstring;
// Returns:
I want to select text up to this point. This says to grab a substring of the text starting FROM character number 1 in the text and continue FOR <whatever character POSITION the first period is found> in the text. We top it all off by giving the entirely new value an ALIAS of 'mySubstring'. The 'mySubstring' will be the index you can use in your PHP code to retrieve the value from the row, such as
$row['mySubstring'].
Now to apply it to your table we simply replace the quoted text with your table's column name (
description) and make sure we select from your table:
SELECT SUBSTRING(description FROM 1 FOR POSITION('.' IN description)) AS mySubstring FROM myTable; This says to grab a substring of the
descriptioncolumn starting FROM character number 1 in the value of the column and continue FOR <whatever character POSITION the first period is found> in the column FROM your table. We used the ALIAS once again as described earlier.
It's truly quite simple once you read and understand the functions. Give it a shot and tell us how you fare.
I certainly learned something from this. I'd still suggest that it may not be that simple for people just starting off in this. However, your points about learning are absolutely 100% correct. It's how I learned (and still do).
There's an old saying: "Good judgment comes from experience. Experience comes from bad judgment."
Don't be afraid to jump off and try things, ask questions, make mistakes and learn from them. Trying first, struggling through, and resolving on your own is very rewarding. Getting stuck and asking for help is inevitable. We all reach these plateaus, if we aren't, we aren't pushing ourselves hard enough. But we need to reach this point by hitting the wall once in awhile. And finding friends in a community to help when we reach this point is priceless. Try. Learn. Return the favor someday.