0

I could use your help on this. I am running a stored procedure in SQL Server 2008 R2 where I import data into an Excel worksheet.

I am able to import the information I need just fine except for the order of the output.

This is what I get:

1 - OUTCO 
10 - OUT 
11 - MATCH 
12 - UNRSL 
2 - INCO 
3 - UNDEL 
4 - MAIL    
5 - NOTSU 
6 - NOMSI 
7 - RSRCH 
9 - IN 
9 - INCOM

I would like to display it with the proper numeric order.

1,2,3,4,5,6,7,9,9,10,11,12.

Here is the code:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [Temp].[dmv_import]
     @date DATE = NULL,
     @import_type VARCHAR(5) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @date_work DATE

    IF @date IS NULL
       SET @date_work = GETDATE()
    ELSE
       SET @date_work = @date 

    SELECT 
        CASE
           WHEN CONVERT(VARCHAR(8), import_code) + ' - ' + vi.import_reason  IS NULL
              THEN 'TOTAL'
              ELSE CONVERT(VARCHAR(8), import_code) + ' - ' + vi.import_reason 
        END AS 'Category',
        SUM(CASE WHEN CONVERT(DATE, vi.createdate, 101) = @date_work THEN 1 ELSE 0 END) AS 'received',
        SUM(CASE WHEN CONVERT(DATE, vi.processed_date, 101) = @date_work THEN 1 ELSE 0 END) AS 'processed'
    FROM 
        DIMSNet.dbo.voter_import vi
    WHERE 
        vi.import_type IN ('DMV', 'DUP','INET') 
        AND import_code < 13
    GROUP BY
        CONVERT(VARCHAR(8), import_code) + ' - ' +vi.import_reason WITH ROLLUP
END

I have tried a few different ORDER BY clauses, but I keep getting conversion errors.

1) ORDER BY CONVERT(INT, LEFT((vi.import_code + ' - ' + vi.import_reason), 2))

2) ORDER BY LEFT((vi.import_code + ' - ' + vi.import_reason),PATINDEX('%[-]%',(vi.import_code + ' - ' + vi.import_reason)-1), CAST(RIGHT((vi.import_code + ' - ' + vi.import_reason), LEN((vi.import_code + ' - ' + vi.import_reason)) - 2) AS INT)

3) ORDER BY (CONVERT(VARCHAR(8), import_code) + ' - ' + vi.import_reason) ASC

4) order by case WHEN ISNUMERIC(vi.import_code + ' - ' + vi.import_reason) = 1 THEN CAST(vi.import_code + ' - ' + vi.import_reason AS FLOAT) WHEN ISNUMERIC(LEFT((vi.import_code + ' - ' + vi.import_reason),1)) = 0 THEN ASCII(LEFT(LOWER((vi.import_code + ' - ' + vi.import_reason)),1)) ELSE 2147483647 End

None of these has returned a properly formatted output.

Any help would be appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Assuming your codes are all numeric, try this:

order by cast(max(import_code) as int)

If not, you can use a case expression:

order by (case when isnumeric(max(import_code)) = 1
               then max(import_code)
          end)

The key here is using the aggregation function to just order by the import_code.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • I Tried both and each time I received this message: "Msg 245, Level 16, State 1, Procedure dmv_import, Line 28 [Batch Start Line 2] Conversion failed when converting the varchar value 'IN' to data type int." – user9909962 Jun 19 '18 at 15:39
  • @user9909962 . . . You are converting `import_code` to a `varchar()` in the `SELECT`. That suggests that it is already a number. In any case, it should be the *number* portion that is used for the `order by`, so I don't see how `'IN'` would be in the data. – Gordon Linoff Jun 20 '18 at 01:26