1

I'm trying to build a SQL statement to retrieve user names in the following order

  • at first, return the names that start with Arabic letter, then the names that start with English letters, then the names that start with special characters.
  • then sort each of the three groups in ascending order.

This is my code:

SELECT `name` FROM `user` 
order by case when substring(name,1,1) like 'N[أ-ي]' then 1
              when substring(name,1,1) like '[a-zA-Z]' then 2
              else 3
              end
        ,name

The problem is that the case part always returns 3, and so the statement sorts the names in the default order(special chars first, then English letters then Arabic letters). What is the problem in my query?

3
  • @Cyval Thanks for your correction.But even when I changed the parameter the function still doesn't work. Commented Jan 3, 2016 at 23:17
  • 2
    nop, this article's example says the substring start index starts with 1... msdn.microsoft.com/hu-hu/library/ms187748.aspx and this too: dev.mysql.com/doc/refman/5.7/en/… Commented Jan 3, 2016 at 23:34
  • @Cyval . . . You are simply wrong. Commented Jan 4, 2016 at 1:20

1 Answer 1

3

You need to use regex, not like... (because you use regular expression)

SELECT `name` FROM `user` 
order by case when substring(name,1,1) regexp 'N[أ-ي]' then 1
              when substring(name,1,1) regexp '[a-zA-Z]' then 2
              else 3
              end
        ,name

Reference: MySQL CASE statement and REGEXP

Sign up to request clarification or add additional context in comments.

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.