Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have the following Stored Procedure that works great EXCEPT when I set the 'OrderBy' to 'OrderNumber'.

I get the following error: Conversion failed when converting the nvarchar value 'SK11270' to data type int.

(SK11270 is a value in the OrderNumber column which is nvarchar(50))

If I run the identical query with any other OrderBy column, it works fine.

I'm completely lost and my head hurts. Can anyone see anything obvious that would be causing this problem?

Thanks in advance for any ideas...

Rich

This works:

sp_jobs '1','20','','JobNumber','ASC','97','True','True','True','True','True','True','True','True','True','False','True','False','True','False','0'

This doesn't:
sp_jobs '1','20','','OrderNumber','ASC','97','True','True','True','True','True','True','True','True','True','False','True','False','True','False','0'

PROCEDURE [dbo].[sp_Jobs] 

@PageNumber int,
@PageSize int,
@FilterExpression varchar(500),
@OrderBy varchar(50),
@OrderDirection varchar(50),
@CustomerID int,
@ShowNotSet bit,
@ShowPlaced bit,
@ShowProofed bit,
@ShowReProofed bit,
@ShowApproved bit,
@ShowOnTime bit,
@ShowLate bit,
@ShowProblem bit,
@ShowCompleted bit,
@ShowDispatched bit,
@ShowUnapproved bit,
@ShowClosed bit,
@ShowReturned bit,
@ShowNoStock bit,
@UserID int

WITH RECOMPILE 

AS

BEGIN
WITH    Keys 
AS (SELECT TOP (@PageNumber * @PageSize) ROW_NUMBER() OVER (
        ORDER BY
            CASE WHEN @OrderDirection = 'ASC' THEN
                CASE 
                  WHEN @OrderBy = 'JobNumber' THEN p1.JobNumber
                  WHEN @OrderBy = 'OrderNumber' THEN p1.OrderNumber 
                  WHEN @OrderBy = 'CustID' THEN p1.CustID
                  WHEN @OrderBy = 'Status' THEN p1.MasterJobStatusID  
                  WHEN @OrderBy = 'DateIn' THEN p1.DateIn
                  WHEN @OrderBy = 'DateDue' THEN p1.DateDue
                  WHEN @OrderBy = 'DateOut' THEN p1.DateOut 
                 ELSE NULL 
                END
            END ASC
            , CASE WHEN @OrderDirection = 'DESC' THEN
                CASE 
                  WHEN @OrderBy = 'JobNumber' THEN p1.JobNumber
                  WHEN @OrderBy = 'OrderNumber' THEN p1.OrderNumber 
                  WHEN @OrderBy = 'CustID' THEN p1.CustID
                  WHEN @OrderBy = 'Status' THEN p1.MasterJobStatusID  
                  WHEN @OrderBy = 'DateIn' THEN p1.DateIn
                  WHEN @OrderBy = 'DateDue' THEN p1.DateDue
                  WHEN @OrderBy = 'DateOut' THEN p1.DateOut           
                 ELSE NULL 
                END
            END DESC)  as rn ,P1.jobNumber,P1.CustID,P1.DateIn,P1.DateDue,P1.DateOut,p1.client,p1.MasterJobStatusID,p1.MasterJobStatusTimestamp,p1.OwnerID,p1.StockComplete,p1.OrderNumber
        FROM vw_Jobs_List P1  WITH (NOLOCK) 
        WHERE (@CustomerID = 0 OR CustID = @CustomerID) AND  (@UserID = 0 OR OwnerID = @UserID) AND ((@ShowNotSet = 1 AND MasterJobStatusID=1) OR (@ShowPlaced = 1 AND MasterJobStatusID=2) OR (@ShowProofed = 1 AND MasterJobStatusID=3)  OR (@ShowReProofed = 1 AND MasterJobStatusID=4)  OR
         (@ShowApproved = 1 AND MasterJobStatusID=5)  OR (@ShowOnTime = 1 AND MasterJobStatusID=6)  OR (@ShowLate = 1 AND MasterJobStatusID=7)  OR (@ShowProblem = 1 AND MasterJobStatusID=8)  OR (@ShowCompleted = 1 AND MasterJobStatusID=9)  OR
         (@ShowDispatched = 1 AND MasterJobStatusID=10) OR (@ShowUnapproved = 1 AND MasterJobStatusID=11)  OR (@ShowClosed = 1 AND MasterJobStatusID=12) OR  (@ShowReturned = 1 AND MasterJobStatusID=13) OR (@ShowNoStock=1 AND StockComplete=0))
         AND (Search LIKE '%'+@FilterExpression+'%')),SelectedKeys

AS  (SELECT TOP (@PageSize)SK.rn,SK.JobNumber,SK.CustID,SK.DateIn,SK.DateDue,SK.DateOut,SK.OrderNumber,SK.MasterJobStatusID
        FROM Keys SK 
        WHERE SK.rn > ((@PageNumber-1) * @PageSize))

        SELECT  SK.rn,J.JobNumber,J.OwnerID,J.Description,J.Client,SK.CustID,SK.OrderNumber, CAST(DateAdd(d, -2, CAST(isnull(SK.DateIn,0) AS DateTime)) AS nvarchar) AS DateIn, CAST(DateAdd(d, -2, CAST(isnull(SK.DateDue,0) AS DateTime)) AS nvarchar) AS DateDue,CAST(DateAdd(d, -2, CAST(isnull(SK.DateOut,0) AS DateTime)) AS nvarchar) AS DateOut, Del_Method,Ticket#, InvoiceEmailed, InvoicePrinted, InvoiceExported, InvoiceComplete, JobStatus,j.MasterJobStatusID,j.MasterJobStatusTimestamp,js.MasterJobStatus,StockComplete
        FROM SelectedKeys SK JOIN vw_Jobs_List J  WITH (NOLOCK)  ON j.JobNumber=SK.JobNumber JOIN tbl_SYSTEM_MasterJobStatus js  WITH (NOLOCK)  ON j.MasterJobStatusID=js.MasterJobStatusID

END
share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

Your CASE expression can only have one datatype. But yours is mixing datatypes and the highest one is being used (as per datatype precedence). So nvarchar is changed to int based on these rules.

You need a case per datatype or per column

Example:

CASE WHEN @OrderDirection = 'DESC' THEN
                 CASE 
                  WHEN @OrderBy = 'JobNumber' THEN J.JobNumber
                  WHEN @OrderBy = 'CustID' THEN J.CustID
                  WHEN @OrderBy = 'DateIn' THEN J.DateIn
                  WHEN @OrderBy = 'DateDue' THEN J.DateDue
                  WHEN @OrderBy = 'DateOut' THEN J.DateOut
                  WHEN @OrderBy = 'Status' THEN J.MasterJobStatusID 
                  ELSE NULL
                END
            END DESC,
CASE WHEN @OrderDirection = 'DESC' THEN
                 CASE 
                  WHEN @OrderBy = 'OrderNumber' THEN J.OrderNumber
                  ELSE NULL
                END
            END DESC
share|improve this answer
    
thanks for your comment. I'm not sure I fully understand. I've modified the SP to remove extraneous order by clauses, and add my p1. to each column. Still not working, what have I missed? –  VaticNZ Jul 27 '11 at 23:23
    
@VaticNZ: read my link then please –  gbn Jul 27 '11 at 23:25
add comment

My fault Vatic, I should of pointed this out in my answer to your earlier question. When using a CASE expression in an order by you have to deal with each data type in a seperate expression. The following is untested but should be closer to what you need.

ORDER BY
    CASE WHEN @OrderDirection = 'ASC' THEN
        CASE @OrderBy -- int
            WHEN 'JobNumber' THEN p1.JobNumber 
            WHEN 'CustID' THEN p1.CustID
            WHEN 'Status' THEN p1.MasterJobStatusID  
        END
    END ASC,
    CASE WHEN @OrderDirection = 'ASC' THEN
        CASE @OrderBy -- datetime
             WHEN 'DateIn' THEN p1.DateIn
             WHEN 'DateDue' THEN p1.DateDue
             WHEN 'DateOut' THEN p1.DateOut  
        END
    END ASC,
    CASE WHEN @OrderDirection = 'ASC' THEN
        CASE @OrderBy -- nvarchar
             WHEN 'OrderNumber' THEN p1.OrderNumber  
        END
    END ASC,
    CASE WHEN @OrderDirection = 'DESC' THEN
        CASE @OrderBy -- int
            WHEN 'JobNumber' THEN p1.JobNumber 
            WHEN 'CustID' THEN p1.CustID
            WHEN 'Status' THEN p1.MasterJobStatusID  
        END
    END DESC,
    CASE WHEN @OrderDirection = 'DESC' THEN
        CASE @OrderBy -- datetime
             WHEN 'DateIn' THEN p1.DateIn
             WHEN 'DateDue' THEN p1.DateDue
             WHEN 'DateOut' THEN p1.DateOut  
        END
    END DESC,
    CASE WHEN @OrderDirection = 'ASC' THEN
        CASE @OrderBy -- nvarchar
             WHEN 'OrderNumber' THEN p1.OrderNumber  
        END
    END DESC
share|improve this answer
    
Thanks guys! I guess we learn something new every day! It also didn't help that it's an inherited database where the original developer is storing DateIn,DateDue,DateOut as integers rather than dates. I've made the changes as you've both suggested and it's working great! Thanks so much for your quick responses. –  VaticNZ Jul 28 '11 at 0:00
1  
Pleasure. Don't forget to accept/vote useful answers :) –  Mark Storey-Smith Jul 28 '11 at 0:09
1  
I've ticked the green icon thingy... is that what you mean? Shame I can't do more than one... –  VaticNZ Jul 28 '11 at 0:18
1  
You can also click the up arrow. You can only accept one answer but you can up-vote all answers that you found helpful. –  Aaron Bertrand Jul 28 '11 at 4:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.