Ordering properties in MS SQL Server - including flats -
i have been given cag , i'm trying sort address postcode , building number. problem face building number not integer, it's nvarchar- because of flat properties.
you can see 79b appears after 143:
131 133a 133b 135 137 139 141 143 79b <-- 87 89 91
i have found similar question: sql-for-ordering-by-number-1-2-3-4-etc-instead-of-1-10-11-12
and tried this
sql += "order buildno * 1 asc ";
but predictably conversion error
conversion failed when converting nvarchar value '133a' data type int.
is possible order type of nvarchar in sql?
thanks
update
i know have working, @paya
select * [" + tblname + "] postcode + @postcode + '%' order cast(left([buildno], case when patindex(n'%[^0-9]%', [buildno]) < 1 len([buildno]) else patindex(n'%[^0-9]%', [buildno]) - 1 end) int), right([buildno], len([buildno]) - patindex(n'%[^0-9]%', [buildno]) + 1)
returns correct order:
79b 87 89 91 133b 135 137 139 141 143
in mssql server
order cast(left([colname], case when patindex(n'%[^0-9]%', [colname]) < 1 len([colname]) else patindex(n'%[^0-9]%', [colname]) - 1 end) int), right([colname], len([colname]) - patindex(n'%[^0-9]%', [colname]) + 1);
Comments
Post a Comment