SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


My BiPolar Users aka Sort a AlphaNumeric Field as if its Numeric


My BiPolar Users aka Sort a AlphaNumeric Field as if its Numeric

Author
Message
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94557 Visits: 38956
Lynn Pettis (1/15/2013)
Can't believe I missed such an easy solution. I guess that's what I get for making things harder than the need to be at first.


Except, I don't think it works.



WITH SampleData (BipolarNumbers) AS (
SELECT '1'
UNION ALL SELECT '100231-A'
UNION ALL SELECT '1003'
UNION ALL SELECT '11'
UNION ALL SELECT '11342'
UNION ALL SELECT '11342-A'
UNION ALL SELECT '12')
SELECT BipolarNumbers
FROM SampleData
ORDER BY CAST(LEFT(BipolarNumbers
,CASE PATINDEX('%[^0-9]%', BipolarNumbers)
WHEN 0 THEN LEN(BipolarNumbers)
ELSE PATINDEX('%[^0-9]%', BipolarNumbers) - 1 END) AS INT);

WITH SampleData (BipolarNumbers) AS (
SELECT '1'
UNION ALL SELECT '100231-A'
UNION ALL SELECT '1003'
UNION ALL SELECT '11'
UNION ALL SELECT '11342'
UNION ALL SELECT '11342-A'
UNION ALL SELECT '11342-B'
UNION ALL SELECT '12')
SELECT BipolarNumbers
FROM SampleData
ORDER BY CAST(LEFT(BipolarNumbers
,CASE PATINDEX('%[^0-9]%', BipolarNumbers)
WHEN 0 THEN LEN(BipolarNumbers)
ELSE PATINDEX('%[^0-9]%', BipolarNumbers) - 1 END) AS INT)
,PATINDEX('%[^0-9]%', BipolarNumbers)




This is what I get as a result:

BipolarNumbers
1
11
12
1003
11342
11342-B
11342-A
100231-A

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17757 Visits: 6431
Lynn Pettis (1/15/2013)
Lynn Pettis (1/15/2013)
Can't believe I missed such an easy solution. I guess that's what I get for making things harder than the need to be at first.


Except, I don't think it works.



WITH SampleData (BipolarNumbers) AS (
SELECT '1'
UNION ALL SELECT '100231-A'
UNION ALL SELECT '1003'
UNION ALL SELECT '11'
UNION ALL SELECT '11342'
UNION ALL SELECT '11342-A'
UNION ALL SELECT '12')
SELECT BipolarNumbers
FROM SampleData
ORDER BY CAST(LEFT(BipolarNumbers
,CASE PATINDEX('%[^0-9]%', BipolarNumbers)
WHEN 0 THEN LEN(BipolarNumbers)
ELSE PATINDEX('%[^0-9]%', BipolarNumbers) - 1 END) AS INT);

WITH SampleData (BipolarNumbers) AS (
SELECT '1'
UNION ALL SELECT '100231-A'
UNION ALL SELECT '1003'
UNION ALL SELECT '11'
UNION ALL SELECT '11342'
UNION ALL SELECT '11342-A'
UNION ALL SELECT '11342-B'
UNION ALL SELECT '12')
SELECT BipolarNumbers
FROM SampleData
ORDER BY CAST(LEFT(BipolarNumbers
,CASE PATINDEX('%[^0-9]%', BipolarNumbers)
WHEN 0 THEN LEN(BipolarNumbers)
ELSE PATINDEX('%[^0-9]%', BipolarNumbers) - 1 END) AS INT)
,PATINDEX('%[^0-9]%', BipolarNumbers)




This is what I get as a result:

BipolarNumbers
1
11
12
1003
11342
11342-B
11342-A
100231-A


Picky! Picky!

How about something simpler then?


WITH SampleData (BipolarNumbers) AS (
SELECT '1'
UNION ALL SELECT '100231-A'
UNION ALL SELECT '1003'
UNION ALL SELECT '11'
UNION ALL SELECT '11342'
UNION ALL SELECT '11342-A'
UNION ALL SELECT '11342-B'
UNION ALL SELECT '12')
SELECT BipolarNumbers
FROM SampleData
ORDER BY CAST(LEFT(BipolarNumbers
,CASE PATINDEX('%[^0-9]%', BipolarNumbers)
WHEN 0 THEN LEN(BipolarNumbers)
ELSE PATINDEX('%[^0-9]%', BipolarNumbers) - 1 END) AS INT)
,BipolarNumbers





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94557 Visits: 38956
Is it wrong to be picky when it's wrong?? Unsure

Still, I should have caught the simpler method. Good work!

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17757 Visits: 6431
Lynn Pettis (1/15/2013)
Is it wrong to be picky when it's wrong?? Unsure

Still, I should have caught the simpler method. Good work!


Of course not. That's the good thing about such a huge community forum. Everybody keeps you honest. :-)


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
david.holley
david.holley
Right there with Babe
Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)

Group: General Forum Members
Points: 752 Visits: 260
Well dang it! I forgot that the whole reason for this is because sometimes they'll take a system generated numeric reference number and create their own using letters as in ...

700234 (System)
700234-A (User)
700234-B (User

And thus the 'A' and 'B' needs to appear below the original. I saw the other posts and will see if something there will work. At least I know that my idea of using two RegEx's was a rather solid solution.
David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16603 Visits: 10109
or...

ORDER BY RIGHT(SPACE(20)+BipolarNumbers,20) ASC




Far away is close at hand in the images of elsewhere.

Anon.


Gazareth
Gazareth
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7708 Visits: 6045
David Burrows (1/16/2013)
or...

ORDER BY RIGHT(SPACE(20)+BipolarNumbers,20) ASC



I was thinking something like that. Didn't get as far as actually writing (or testing) it though :-)
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17757 Visits: 6431
David Burrows (1/16/2013)
or...

ORDER BY RIGHT(SPACE(20)+BipolarNumbers,20) ASC



+1


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search