David Garcia
David Garcia

Reputation: 2696

MySQL ReGex statement

I have never come across RegEx in mySQL, In a column I have over 500 different server versions. is it possible to only fetch anything before a forward slash so that i can detect the most common servers by filtering out the versions?

Microsoft-IIS/7.5
Microsoft-IIS/6.0
Apache/2.2.24 (Unix)
Apache/2.2.15 (CentO
Apache/1.3.42 (Unix)
Apache/2.2.22 (Ubunt
Resin/3.0.23
Apache/2.2.0 (Linux/

Upvotes: 0

Views: 57

Answers (2)

John Woo
John Woo

Reputation: 263733

here,

SELECT DISTINCT SUBSTRING(version,1,LOCATE('/', version)-1) FROM TableName

because of DISTINCT, only unique rows are displayed.

Upvotes: 2

zerkms
zerkms

Reputation: 254944

In mysql regex cannot return a matched substring, but only return a boolean if the string matches a regular expression or not.

What you need is just a SUBSTR

SUBSTRING(col, 1, LOCATE('/', col) - 1)

but keep in mind that if there is no / in the string you'll end up with an empty string as a result

Upvotes: 2

Related Questions