How would I do a natural sort of a column that has c then number so example
C2458
C2459
C2581
C2594
C2712
C1048
C1303
C1146
C1658
C2453
C2830
C3071
C3143
C3144
C3702
C1018
C1019
C1020
C1021
C1015
C1016
C1024
C1026
C1027
C1028
C1033
C1034
C1152
C1157
C1249
C1250
C1251
C1252
C1253
C1254
C1255
C1256
C1257
C1258
C1259
C1260
C1261
C1262
C1263
C1264
C1265
C1266
C1267
C1268
C1269
C1270
C1271
C1272
C1273
C1274
C1275
C1276
C1277
C1278
C1279
C1280
C1281
C1282
C1283
C1284
C1285
C1286
C1287
C1288
C1289
C1290
C1291
C1292
C1293
C1294
C228
C229
C240
C243
C517
C518
C521
C522
C793
C794
C795
C796
C852
C928
C929
C930
C931
C941
C922
C923
C924
C925
C331
C945
C958
C961
C1059
C1060
C964
C950
C951
C954
C985
C994
C878
C879
C1295
C1040
C2902
C1149
C1592
C2711
C1053
C1054
C1056
C1058
C1139
C1683
C1684
C1012
C926
C967
C949
C957
C966
C980
C1000
C1001
C1007
C2709
C927
C942
C943
C948
C2486
C1029
C1030
C978
C944
C946
C947
C952
C953
C955
C956
C959
C960
C962
C963
C993
C965
C968
C1009
C969
C970
C982
C971
C979
C981
C983
C984
C986
C987
C995
C1002
C1011
C1003
C1006
C1008
C1010
C1013
C1014
C1017
C1022
C1023
C1025
C1031
C1032
C1035
C1036
C1041
C1039
C1047
C1037
C1038
C1042
C1043
C1044
C1046
C1045
C1296
C1297
C1298
C1655
C1662
C1782
C1844
C1845
C1846
C1885
C1889
C1890
C988
C515
C244
C241
C230
C231
C1061
C1063
C1064
C1065
C1067
C1068
C1069
C1070
C1071
C1072
C1073
C1074
C1075
C1076
C1077
C1078
C1079
C1080
C1081
C1082
C1083
C1084
C1085
C1086
C1087
C1088
C1089
C1090
C1091
C1092
C1093
C1094
C1095
C1096
C1097
C1098
C1099
C1100
C1101
C1102
C1103
C1104
C1105
C1106
C1107
C1108
C1109
C1110
C1111
C1112
C1113
C1114
C1115
C1116
C1117
C1118
C1119
C1120
C1121
C1122
C1123
C1124
C1125
C1126
C1127
C1128
C1129
C1130
C1131
C1132
C1133
C1134
C1135
C1136
C1137
C1138
C1141
C1837
C2547
It would sort like least to greatest or greatest to least
CodePudding user response:
We can try sorting on the digit component of the string, e.g.
SELECT *
FROM yourTable
ORDER BY CAST(RIGHT(col, LEN(col) - 1) AS INT);
Note that the cast to integer in the ORDER BY
clause is very necessary assuming you want to sort numerically rather than lexicographically (as text).
CodePudding user response:
Would using the below work for you?
SELECT FOO FROM BAR
WHERE X IN Y
ORDER BY RIGHT(FOO,LEN(FOO)-1) DESC
Please note this is untested.