1

Straight to the point and this might be very simple for some of you.

I have a simple SELECT query (select description from table) which produces all i want like below :

 - testword123
 - testword875
 - myjob1 45
 - myjob is 544

What i need is to have as a result :

 - testword
 - myjob

I can use a SELECT distinct LEFT(description,8) which works fine, but the problem is not ALL 'description' have the same number of words :-(

So basically, what i want is retrieve ONLY the letters from the 'description' result set.

Thanks!!

R

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Richard H.
  • 11
  • 1
  • SELECT distinct LEFT(description,8) wouldn't work for the example provided above. Can you provide a more realistic/representative data set? – Strawberry Dec 11 '15 at 10:04
  • >but the problem is not ALL 'description' have the same number of words That is the problem. You might want to look into your database design. – KaeL Dec 11 '15 at 10:06
  • try http://stackoverflow.com/questions/11134452/find-cut-string-from-first-integer-in-mysql – undefined_variable Dec 11 '15 at 10:16
  • Thanks "undefined_variable" - Your solution "http://stackoverflow.com/questions/11134452/find-cut-string-from-first-integer-in-mysql" was the correct one!! (y) (with a little bit of tweaking, this helped A LOT) A+++ – Richard H. Dec 11 '15 at 12:23

3 Answers3

2
SELECT distinct LEFT(description, charindex(' ', description) - 1)

Depending on your implementation, it might be possible to declare 'description' as a variable beforehand so you don't have to type it twice in the same query.

Rick
  • 45
  • 5
0

There are two decisions: 1) Handle each decription in PHP or 2) Handle user input before writing it to DB. Add field to table as index of first not letter symbol and then use it in LEFT mysql function

Alexus
  • 111
  • 3
0

Thanks "undefined_variable" - Your solution "stackoverflow.com/questions/11134452/…; was the correct one!! (y) (with a little bit of tweaking, this helped A LOT) A+++

Richard H.
  • 11
  • 1