3

I have a query in MYSql database. I have a table order_det, the table's column remarks_desc contains the entries as follows:

Table structure:

Table: order_det

Columns: rec_id, remarks_desc

Sample records in order_det table

rec_id      remarks_desc
_________________________________________________________

1           a specific PROGRAMMING problem
2           A software Algorithm
3           software tools commonly USED by programmers
4           Practical, answerable problems that are unique to the programming profession
5           then you’re in the right place to ask your question
6           to see if your QUESTION has been asked BEFORE

My requirement I want to select only the records which that contains one more more words stored in all uppercase letters. From the above 6 records, I want to select only below 1,3,6 records:

rec_id      remarks_desc
__________________________________________________
1           a specific PROGRAMMING problem (it contains one all uppercase word PROGRAMMING)
3           software tools commonly USED by programmers (it contains one all uppercase word USED)
6           to see if your QUESTION has been asked BEFORE (it contains two all uppercase words QUESTION and BEFORE)

I tried to archive this using LIKE, REGEXP but getting incorrect result. Please help me to get the correct result.

2 Answers 2

0

Try:

SELECT rec_id, remarks_desc FROM order_det WHERE remarks_desc REGEXP '(^|[[:blank:]])[[:upper:]][[:upper:]]+([[:blank:]]|$)'

I have assumed that you want to exclude single-letter capitalised words. If you want to exclude capitalised words at the start of the string, you'll need to tweak the regex.

Make sure that your table collation is case sensitive (_cs not _ci)

I used information from http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

However, if you're having to use regular expressions to extract data from a database, it's worth considering whether your database design could be improved. This is particularly important if you need good performance from the database.

2
  • Tim, yes the table collation is case sensitive. But it is not not exactly returns the output, gives including initcaps words that entry doesn't have fully caps words.
    – Manim
    Oct 31, 2012 at 14:06
  • Sorry, I don't quite understand your comment. Can you give me an example of something my query matches that you don't want to match?
    – Tim Bellis
    Oct 31, 2012 at 14:57
0

Here is the pretty straight forward stored function which returns amount of words in uppercase in row.

Cons:

  • it's stored function not pure SQL;
  • it uses collate
  • it uses regexp, but you can fill free to get rid of it using another inner loop for it;
  • it counts all words but you can add break if you reach 2.

Please find the function on the following link (gist.github.com). It doesn't display correctly here.

2
  • Ravnur, I didn't use this since I need to create function but I will try this.
    – Manim
    Oct 31, 2012 at 14:05
  • I'm not sure that you can do what you want using pure sql
    – ravnur
    Oct 31, 2012 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.