I have a table which contains a list of domain names and a description.
I wish to use a simple function (native function if possible) for reversing the domain name notation for use in a better, more natural sort order (personal opinion).
For example, I want to have the following data converted from left to right:
admin.test.example.com => com.example.test.admin
api.test.example.com => com.example.test.api
cdn.test.example.com => com.example.test.cdn
test.example.com => com.example.test
admin.staging.example.com => com.example.staging.admin
api.staging.example.com => com.example.staging.api
cdn.staging.example.com => com.example.staging.cdn
staging.example.com => com.example.staging
To do this in PHP I could do something simple like:
$rev_domain = implode('.', array_reverse(explode('.', $domain)));
Is this easily possible in SQL? (prefer SQL standards compliance, or MySQL if extensions are required, or both if you happen to know both).
Clearly another option is to create and maintain a new column which contains the reversed DNS name, however I am looking for something which can be used on-the-fly in a query.
Thanks in advance!
Edit: I'm looking for a solution which will work for any count of sub-domain parts, example: example.com and 1.2.3.4.5.6.7.8.9.0.example.com