SQL Convert Function

Last post 07-18-2008 3:34 PM by santa_1975. 2 replies.

Sort Posts:

  • SQL Convert Function

    07-18-2008, 2:30 PM
    • Member
      10 point Member
    • oceamus
    • Member since 07-14-2008, 4:18 PM
    • Posts 18

     Hi,

    Im trying to convert an integer to a varchar in a stored procedure on line 6. But for some reason I keep getting the same error msg when I try to load the page.


    Syntax error converting the varchar value '78 AV SE' to a column of data type int.

     

     

    SELECT
    OrderNo,
    ServiceTypes.ServiceCode AS 'Svc: ',
    --AccountNumber,
    PickupCompanyName AS 'P: ',
    Convert(Varchar(10),PickupStreetNo) + ' ' + PickupStreet + '  '+ CASE When PickupUnit is Null Then ISNULL(PickupUnit, '') Else ' Unit '+ PickupUnit END AS 'P Address: ',
    --PickupCity,
    --PickupPostalCode,
    ISNULL(PickupClose, '') AS 'Pic Close: ',
    ISNULL(Instructions,'') AS 'Instr: ',
    DeliveryCompanyName AS 'D: ',
    Convert(Varchar(10),DeliveryStreetNo) +' ' + DeliveryStreet + ' ' + CASE When DeliveryUnit is Null Then ISNULL(DeliveryUnit, '') Else ' Unit '+DeliveryUnit END AS 'D Address: ',
    --DeliveryCity,
    --DeliveryPostalCode,
    ISNULL(DeliveryClose, '') AS 'Del Close: ',
    ReadyTime AS 'Ready: ',
    DeliverBy AS 'Del By: ',
    Weight AS 'Weight: ',
    (SELECT COUNT(*) FROM OrderPackages WHERE OrderNo = @OrderNo_1
    ) AS 'Pieces: '
    --StatusID
     FROM orders
    INNER JOIN ServiceTypes ON ServiceTypes.ServiceTypeID = Orders.ServiceTypeID
    WHERE
    OrderNo = @OrderNo_1

    Does anyone have any idea why this might be happening? Is my convert syntax wrong?

  • Re: SQL Convert Function

    07-18-2008, 3:28 PM
    • Member
      406 point Member
    • shbrat
    • Member since 12-09-2007, 4:18 AM
    • Posts 93

    Is Pickupunit of datatype INT? If so, then the problem is in ELSE part of the case. It is unable to convert PickupUnit to varchar and concat it:

    CASE When @PickupUnit is Null

    Then ISNULL(@PickupUnit, '')

    Else ' Unit '+ @PickupUnit

    END AS 'P Address'

    Try to check for the value of PickupUnit and then convert that to VARCHAR, like so:

    CASE When ISNULL(@PickupUnit, 0) = 0

    Then ''

    Else ' Unit '+ Convert(VARCHAR(10), @PickupUnit )

    END AS 'P Address'

  • Re: SQL Convert Function

    07-18-2008, 3:34 PM
    • Contributor
      2,980 point Contributor
    • santa_1975
    • Member since 06-30-2008, 2:20 AM
    • Posts 496

    Use CAST() instead of CONVERT(). It should work.

    Refer to the link below for more information on this.

    http://msdn.microsoft.com/en-us/library/aa226054.aspx

    Hope this helps.

     

Page 1 of 1 (3 items)