Issue with cast and convert function in the update statement

  • Hi,

    I have a query with update statement

    UPDATE #Rpt

    SET Productfamily = PD.pt_sp_type_c

    FROM #Rpt s

    INNER JOIN #ProductDetails PD on PD.pt_sb_type_c = s.CONVERT(VARCHAR(10),productId) --03112011

    here pt_sp_type_c and pt_sb_type_c both are varchar(10) and productId is integer

    so when i am trying to execute teh query its showing that cannot convert varchar to int..i am changing productId to varchar for that i am using

    cast and convert for me its showing incorrect syntax near the keyword convert

    whern i use cast s.CAST(productId AS VARCHAR(10)) --here its showing incorrect syntax near keyword AS can anyone come with appropraite answer..

  • Try CAST(s.productId AS VARCHAR(10)). The way you had it made it think that CAST is a column in s. Your convert would work the same way.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • shalini72011 (11/7/2011)


    Hi,

    I have a query with update statement

    UPDATE #Rpt

    SET Productfamily = PD.pt_sp_type_c

    FROM #Rpt s

    INNER JOIN #ProductDetails PD on PD.pt_sb_type_c = s.CONVERT(VARCHAR(10),productId) --03112011

    here pt_sp_type_c and pt_sb_type_c both are varchar(10) and productId is integer

    so when i am trying to execute teh query its showing that cannot convert varchar to int..i am changing productId to varchar for that i am using

    cast and convert for me its showing incorrect syntax near the keyword convert

    whern i use cast s.CAST(productId AS VARCHAR(10)) --here its showing incorrect syntax near keyword AS can anyone come with appropraite answer..

    And what is the data-type of ProductFamily?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply