Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this is a bit old at this point, but in my search for a better solution, I came across this question. I'm currently using a function to order by. It works fine for my purpose of sorting records which are named with mixed alpha numeric ('item 1', 'item 10', 'item 2', etc) </p> <pre><code>CREATE FUNCTION [dbo].[fnMixSort] ( @ColValue NVARCHAR(255) ) RETURNS NVARCHAR(1000) AS BEGIN DECLARE @p1 NVARCHAR(255), @p2 NVARCHAR(255), @p3 NVARCHAR(255), @p4 NVARCHAR(255), @Index TINYINT IF @ColValue LIKE '[a-z]%' SELECT @Index = PATINDEX('%[0-9]%', @ColValue), @p1 = LEFT(CASE WHEN @Index = 0 THEN @ColValue ELSE LEFT(@ColValue, @Index - 1) END + REPLICATE(' ', 255), 255), @ColValue = CASE WHEN @Index = 0 THEN '' ELSE SUBSTRING(@ColValue, @Index, 255) END ELSE SELECT @p1 = REPLICATE(' ', 255) SELECT @Index = PATINDEX('%[^0-9]%', @ColValue) IF @Index = 0 SELECT @p2 = RIGHT(REPLICATE(' ', 255) + @ColValue, 255), @ColValue = '' ELSE SELECT @p2 = RIGHT(REPLICATE(' ', 255) + LEFT(@ColValue, @Index - 1), 255), @ColValue = SUBSTRING(@ColValue, @Index, 255) SELECT @Index = PATINDEX('%[0-9,a-z]%', @ColValue) IF @Index = 0 SELECT @p3 = REPLICATE(' ', 255) ELSE SELECT @p3 = LEFT(REPLICATE(' ', 255) + LEFT(@ColValue, @Index - 1), 255), @ColValue = SUBSTRING(@ColValue, @Index, 255) IF PATINDEX('%[^0-9]%', @ColValue) = 0 SELECT @p4 = RIGHT(REPLICATE(' ', 255) + @ColValue, 255) ELSE SELECT @p4 = LEFT(@ColValue + REPLICATE(' ', 255), 255) RETURN @p1 + @p2 + @p3 + @p4 END </code></pre> <p>Then call</p> <pre><code>select item_name from my_table order by fnMixSort(item_name) </code></pre> <p>It easily triples the processing time for a simple data read, so it may not be the perfect solution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload