0
SELECT ProductNumber, Name, ListPrice
FROM SalesLT.Product 
WHERE ProductNumber LIKE 'BK-[^R]%-[0-9][0-9]';'BK-M47B-38'

I want product that is: start with 'BK-' followed by any character other than 'R', and ends with a '-' followed by any two numerals. Above is ms-sql query and I want mysql version of this query productNumber is like below :

'BK-M47B-40'
'BK-M82B-44'
'FR-R38B-60'
'ST-9828'

2 Answers 2

1

The equivalent regular expression is pretty similar:

SELECT ProductNumber, Name, ListPrice
FROM SalesLT.Product 
WHERE ProductNumber REGEXP '^BK[-][^R].*[-][0-9]{2}$'

Key differences:

  • ^ and $ mark the beginning and end of string (otherwise, regular expressions match anywhere in the string.
  • % --> .*
  • The {2} is optional but more colloquial in regular expressions for repeating patterns. Note: you could also use [:digit], but that's longer to type.
  • I forget whether a hyphen is a special character or not, so I just put it in square brackets.
Sign up to request clarification or add additional context in comments.

Comments

1

Use RLIKE:

SELECT *
FROM Product 
WHERE ProductNumber RLIKE '^BK-[^R].*-[0-9][0-9]$';

SqlFiddleDemo

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.