can casting bigint into varchar(20) truncate or overflow.

Answered can casting bigint into varchar(20) truncate or overflow.

  • Sunday, April 15, 2012 7:03 AM
     
     

    Dear all

    i have a table and a column

    create table t1 ( col1 bigint)

    ex: select cast(col1 as varchar(20))  as eid from t1


    this query is working fine for -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)

    my doubt is there any situation (setting) where varchar(20) needs to be extended?

    yours sincerlly.




All Replies

  • Sunday, April 15, 2012 7:26 AM
    Moderator
     
     

    I don't think so. But if you change it into varchar(32) you can surely sleep without overflow worries.

    SQL number types:

    http://www.sqlusa.com/bestpractices2008/sql-number/


    Kalman Toth SQL SERVER & BI TRAINING

  • Sunday, April 15, 2012 10:23 AM
     
     Answered Has Code

    Hello,

    it should word well. Did you experience any problem?

    DECLARE 
     @bigint_low BIGINT = -9223372036854775808
    ,@bigint_high BIGINT = 9223372036854775807
    ,@varchar_low VARCHAR(20) 
    ,@varchar_high VARCHAR(20)
    
    SET @varchar_low = CAST(@bigint_low AS VARCHAR(20))
    SET @varchar_high = CAST(@bigint_high AS VARCHAR(20))
    
    SELECT @varchar_low AS VARCHAR_LOW, @varchar_high AS VARCHAR_HIGH

    • Marked As Answer by rajemessage Monday, April 16, 2012 3:54 AM
    •