2

I'm trying to query and sort a column without much luck. My data looks like this and is consistent with the preceding "WP".

Should display as

  WP1-WP2
    WP2-WP3
    WP10-WP11
    WP10-WP12

Actually displays as

 WP1-WP2
    WP10-WP11
    WP10-WP12
    WP2-WP3

I've attempted adapting several answers found on here with no success.

Question is how do I sort this list as it should??

EDIT: I am using distinct in the query, not sure if this will effect the problem I'm having

3
  • @juergen d Thanks, I was in the middle of doing that :) Commented Apr 2, 2014 at 9:47
  • Which is your database? SQL Server, MySql, Oracle etc. Commented Apr 2, 2014 at 9:55
  • MySQL according to 000webhost's website Commented Apr 2, 2014 at 11:10

2 Answers 2

2

Try this,

 select *
 from myStringSorting
order by Cast(Replace(Substring(mycol,0,CHARINDEX('-',mycol)),'WP','') as Int) asc
        ,Cast(Replace(Substring(mycol,CHARINDEX('-',mycol) + 1,LEN(mycol)),'WP','') as Int) asc

Check out this link, http://sqlfiddle.com/#!3/ca45f/5

With DISTINCT,

With CTE as
(
select distinct mycol
 from myStringSorting
 )
 select * from CTE
order by Cast(Replace(Substring(mycol,0,CHARINDEX('-',mycol)),'WP','') as Int) asc
        ,Cast(Replace(Substring(mycol,CHARINDEX('-',mycol) + 1,LEN(mycol)),'WP','') as Int) asc

With DISTINCT, without CTE, using sub query,

Select a.mycol from
(
select distinct mycol
 from myStringSorting
 )as a
order by Cast(Replace(Substring(a.mycol,0,CHARINDEX('-',a.mycol)),'WP','') as Int) asc
        ,Cast(Replace(Substring(a.mycol,CHARINDEX('-',a.mycol) + 1,LEN(a.mycol)),'WP','') as Int) asc
Sign up to request clarification or add additional context in comments.

12 Comments

Syntax problem, not sure what ver SQL is being used, using 000webhost.
Corrected, please check.
SQL query: Documentation SELECT DISTINCT WP FROM btsec WHERE Estimate = 'IBH39WMT' ORDER BY Cast( Replace( Substring( WP, 0, CHARINDEX( '-', WP ) ) , 'WP', '' ) AS Int ) ASC , Cast( Replace( Substring( WP, CHARINDEX( '-', WP ) +1, LEN( WP ) ) , 'WP', '' ) AS int ) ASC LIMIT 0 , 30 MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Int) asc,Cast(Replace(Substring(WP,CHARINDEX('-',WP) + 1,LEN(WP)),'WP','') as in' at line 1
It doesn't seem to like what comes after CHARINDEX( from ' onwards
Where are you executing this code? SQl Fiddle or your server?
|
1

Another method

select * from myStringSorting
order by len(mycol),mycol

2 Comments

I have tried this one and it did throw and error. #1547 - Column count of mysql.proc is wrong. Expected 20, found 16. The table is probably corrupted
LENGTH(mycol),mycol works but LEN(mycol),mycol doesn't. Fussy version of SQL on the server it seems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.