Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

In SQL 2005, Cast to varchar says 'Can't cast value to data type int'?

Back to Forum: Tech Off
  • User profile image
    qwert231

    Here's my query and the resulting error:

    SELECT
    PhotoID = CAST(ShoppingCartDetails.PhotoID AS varchar(50))
    FROM ShoppingCartDetails
    LEFT JOIN Photos ON ShoppingCartDetails.PhotoID = Photos.PhotoID AND Photos.ImageOrderNum = @ImageOrderNum
    LEFT JOIN SubjectData ON ShoppingCartDetails.SubjectID = SubjectData.SubjectID
    LEFT JOIN CartPhotos ON ShoppingCartDetails.PhotoID = CartPhotos.PhotoID AND ShoppingCartDetails.ShoppingCartID = CartPhotos.ShoppingCartID
    WHERE ShoppingCartDetails.ShoppingCartID = @ShoppingCartID
    AND ShoppingCartDetails.Qty > 0
    AND ShoppingCartDetails.PhotoID IS NOT Null

    Msg 245, Level 16, State 1, Line 11
    Conversion failed when converting the varchar value 'DLB002' to data type int.


    What gives? Obviously DLB002 is not int. But where am I trying to convert it to int? Shouldn't CAST work still? (ShoppingCartDetails.PhotoID is set as varchar(50))

  • User profile image
    Maurits

    Are any of these comparisons between an int and a varchar(...), which would lead to an implicit conversion?

    ShoppingCartDetails.PhotoID = Photos.PhotoID
    Photos.ImageOrderNum = @ImageOrderNum
    ShoppingCartDetails.SubjectID = SubjectData.SubjectID
    ShoppingCartDetails.PhotoID = CartPhotos.PhotoID
    ShoppingCartDetails.ShoppingCartID = CartPhotos.ShoppingCartID
    ShoppingCartDetails.ShoppingCartID = @ShoppingCartID
    ShoppingCartDetails.Qty > 0

  • User profile image
    qwert231

    Yea, it was the first one...

    I wish I didn't have to use varchars as IDs, but we integrate w/ a system that does.

    Thanks, it's fixed.

  • User profile image
    littleguru

    qwert231 wrote:
    Yea, it was the first one...

    I wish I didn't have to use varchars as IDs, but we integrate w/ a system that does.

    Thanks, it's fixed.


    Are they using GUIDs for the IDs?

  • User profile image
    JuNK64

    This is my stored proc prepared on mssql 2005
    ---------------------------------------------
    ALTER PROCEDURE JK_USRLOGIN

    (

    @USR VARCHAR(20),

    @PWD VARCHAR(20)

    )

    AS

    BEGIN

    DECLARE @AD VARCHAR(50)

    DECLARE @SOYAD VARCHAR(50)

    DECLARE @YETKI INT

    SELECT @AD=AD,@SOYAD=SOYAD FROM USRS WHERE USR=@USR AND PWD=@PWD

    return( @AD)

    return( @SOYAD)

    END

    GO
    ------------------------------
     I get the error message
    -------------------

    Msg 245, Level 16, State 1, Procedure JK_USRLOGIN, Line 14

    Conversion failed when converting the varchar value 'xxx' to data type int.

    -------------------
    But sp execute normally
    haow can ı solve this please help its emergency

  • User profile image
    Matthew van Eerde

    RETURN only takes integers, not strings.  You probably want
    SELECT AD, SOYAD FROM USRS WHERE USR=@USR AND PWD=@PWD

    instead.

    Or you might consider having @AD and @SOYAD being output parameters instead.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.