There is a string, sort the letters (characters) of the string alphabetically.

Example:

SET @var="acb"; SELECT @var; SELECT SORT(@var); 

Conclusion:

acb @var;

abc SORT(@var);

  • And what is a "character set", in SQL it is customary to store any "sets" one item per line and then the usual order by will sort everything out perfectly - Mike
  • variable or field value - Los Pollos Hermanos
  • example: SET @var=СОРТ("авб") ; - Los Pollos Hermanos
  • SQL is not intended for such operations. In principle, of course you can, but it's not worth it. there will be a three-story query that expands the string into separate letters, sorts and collects back. it would be better if such things are not done in SQL or it is possible to reconsider the approach to the task, which would not be necessary in principle - Mike
  • although at the expense of a 3-storey building, I certainly got excited, but it is still not worth it imho - Mike

1 answer 1

We will need a worksheet with ordinal numbers from 1 to the maximum length of the line being processed:

 create table seqnum(X int not null, primary key(X)); insert into seqnum values(1),(2),(3),(4); insert into seqnum select X+4 from seqnum; insert into seqnum select X+8 from seqnum; insert into seqnum select X+16 from seqnum; insert into seqnum select X+32 from seqnum; ... 

The sorting query will look like this:

 SET @var="acb"; select group_concat(substr(@var,x,1) order by substr(@var,x,1) separator '') from seqnum where x<=length(@var); 

This query first expands the input string into individual characters, one per record, and then collects the resulting sample back into the string, but in the specified order.