sql - Sorting a varchar column in customized way -


i trying sort varchar column contains data this:

a.1)    null a.1.xc) 1131820 b.1)    null b.1.xc) 1131822 c.1)    null c.1.xc) 131824 c.2) (ce)   null c.2) (nrml) null c.2.xc) 131826 c.2.xc) 132152 c.3)    null c.3.a)  131828 c.3.a.xc)   131830 c.3.xc) 131828 c.4)    null c.4.a)  131838 c.4.a.xc)   131840 c.4.xc) 131838 d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1)    null d.1.xc) 16131842 d.1.xc) 15131842 d.1.xc) 14131842 d.1.xc) 13131842 d.1.xc) 12131842 d.1.xc) 11131842 d.1.xc) 10131842 d.1.xc) 9131842 d.1.xc) 8131842 d.1.xc) 7131842 d.1.xc) 6131842 d.1.xc) 5131842 d.1.xc) 4131842 d.1.xc) 1131842 d.1.xc) 3131842 d.1.xc) 2131842 d.2)    null d.2.xc) 132124 d.3)    null d.3.xc) 132126 d.4)    null d.4.xc) 1132156 d.5) (nrml) null d.5.xc) 132158 e.1)    null e.1.xc) 132138 e.10)   null e.10.xc)    131932 e.10.xf)    131932 e.10.xl)    131932 e.11) (nrml)    null e.11.xc)    131939 e.11.xf)    131939 e.11.xl 131939 e.12.a) null e.12.a.xc)  131965 e.12.a.xl)  131965 e.13)   null e.13.a) 131988 e.13.a.xc)  131990 e.13.xc)    131988 e.14)   null e.14.xc)    131994 e.14.xl)    131994 e.15)   null e.15.xc)    132012 e.16)   null e.16.xc)    132014 e.17.a) (allfnds)   null e.17.a.xc)  132016 e.17.a.xf)  132016 e.18)   null e.18.xc)    132022 e.2)    null e.2.xc) 131844 e.3)    null e.3.xc) 131850 e.4)    null e.4.xc) 131856 e.5)    null e.5.xc) 131862 e.6)    null e.6.xc) 131868 e.7)    null e.7.a)  131874 e.7.a.xc)   131876 e.7.b)  131874 e.7.b.i)    131878 e.7.b.i.xc) 131886 e.7.b.xc)   131878 e.7.xc) 131874 e.8) (nrml) null e.8.xc) 131890 e.9) (nrml) null e.9.a)  131908 e.9.a.xc)   131910 e.9.a.xf)   131910 e.9.a.xl)   131910 e.9.xc) 131908 

i using below query sort column

select a,bfrom abcd id =18613 order 

now query giving me issue e.1.xc), expecting e.2) returning me e.10) , on.

column varchar column.

i tried query no luck

select   case    when isnumeric(a)=1    cast(a int)    when patindex('%[^0-9]%',a) > 1    cast(     left(       a,       patindex('%[^0-9]%',a) - 1     ) int)    else 2147483648 end,    case    when isnumeric(a)=1    null    when patindex('%[^0-9]%',a) > 1    substring(       a,       patindex('%[^0-9]%',a) ,       50     )     else end abcd id=18613 order 

also note after d.5) contains theb null wan sub branch d.5 xc) having b column 132158.

sample sql fiddle

try this. uses parsename function sql split object name it's various parts, split dots...

select a, b (     select a, b,      case         when len(a) - len(replace(a, '.', '')) = 3         when patindex('%.', replace(a, ')', '.')) > 0 replace(a, ')', '.') + 'x'         else replace(a, ')', '.')     end dummy     abcd) data     order left(data.a, 1),      convert(int, case         when isnumeric(parsename(data.dummy, 4)) = 1 parsename(data.dummy, 4)         when isnumeric(parsename(data.dummy, 3)) = 1 parsename(data.dummy, 3)         when isnumeric(parsename(data.dummy, 2)) = 1 parsename(data.dummy, 2)         when isnumeric(parsename(data.dummy, 1)) = 1 parsename(data.dummy, 1)     end), data.a 

this query might not cater permutations in data, it's start. idea.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -