Casting a CHAR to an INT

  • I'm having trouble casting a CHAR field to an INT. I keep getting a syntax error message:

    Server: Msg 245, Level 16, State 1, Line 1

    Syntax error converting the varchar value 'K' to a column of data type int.

    Here is the code:

    Update tblIntelecDownload

    Set CRSC_3_PLACE_NUM = 1

    WHERE CAST(SCRCTG AS INT) = 3 

    Can someone please tell me what I'm doing wrong.

    Thanks in advance!

     

     

  • Can you do it that way?

    Update tblIntelecDownload

    Set CRSC_3_PLACE_NUM = 1

    WHERE SCRCTG = '3'

  • There may be rows with nonumeric values in the column in question.

    check

    SELECT SCRCTG, * FROM tblIntelecDownload

    WHERE

     ISNUMERIC(SCRCTG) = 0

     

    Good idea Remi

    Regards,
    gova

  • Gonna have to learn to type slower

    follow up question :

    Are you casting to int because you have values like '03' or '0003'?

  • Man...I feel so stupid! Premature senior moment. It never crossed my mind to just put my criteria in quotes.

    Thanks Remi

  • Gives a chance to use an index seek too .

  • beware

    "SELECT SCRCTG, * FROM tblIntelecDownload WHERE ISNUMERIC(SCRCTG) = 0"

    won't deal with some 'char' data...ie numbers which can be represented using "scientific notation" ....particularly D and E

  • It's better to use LIKE and wildcards instead of ISNUMERIC() because LIKE + wc gives you more control.

    If you want to find values with numerics only:

    SELECT myColumn

    FROM   myTable

    WHERE  myColumn NOT LIKE '%[^0-9]%'

    If you want to find values that have any chars but numerics only:

    SELECT myColumn

    FROM   myTable

    WHERE  myColumn LIKE '%[^0-9]%'

    /Kenneth

     

Viewing 8 posts - 1 through 7 (of 7 total)

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