Convert integer

  • Hello, I get error message from this code:

    set @Number = CONVERT(INT,@Number))

    The conversion of the varchar value '2011060910105711742 ' overflowed an int column. Maximum integer value exceeded.

    How can I fix it?

    Thank you

  • you'll need a larger data type...bigint might hold it.

    the largest int is ~2 billion...your value's a few orders larger than that.

    select convert(bigint,2011060910105711742)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thank you,but I still get the same error when I do this

    case when

    [section] = 'C'

    then

    @Number

    when [section] = 'A' then @Number + 1

    else null

    end as [Number]

  • where is the DECLARE @Number bigint statement?

    where is the SET Number = CONVERT(bigint,????) statment?

    what you posted is disconnected from my perspective...iu think you need to show more of the offending code.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • We definitely need to see not only more code (because what you posted doesn't give us enough information), but we need to see table structure and sample data (if the @Number is coming from a table).

    Where are you getting such a large number from anyway? EDIT: It almost looks like a faux datetime stamp.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Sorry,here is my code:

    declare @Number char(20);

    set @Number = convert(nvarchar(50),rand())

    set @Number = '1' + substring(@Number,3,4)

    set @Number = convert(int,@Number)

    DECLARE @dd VARCHAR(20)

    SET @dd = CONVERT(VARCHAR(20), GETDATE(), 20)

    PRINT '

    current date:' + @dd

    SET @dd = REPLACE ( REPLACE ( REPLACE ( @dd, '-', ''), ':', ''), ' ' , '')

    PRINT '

    formatted

    current date:' + @dd

    SET @Number = @dd + @Number

    PRINT '

    @Number

    :' + @Number

    select

    case when

    [Type] = 'C'

    then

    @Number

    when [Type] = 'A' then @Number + 1

    else null

    end as [Number] into table1

  • Ok, what's the point here?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • i AM TRYING NOT TO get a duplicate key IN the record. I need to make follow a format of yymmddrrrrr where yymmdd is year-month-day and rrrrr is the existing random number.

  • Wouldn't an identity column be easier? Or even a GUID?

    Why this convoluted approach?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • They don't like big numbers and but I am wondering if this bases on each time interface runs generates unique number

  • Hang on... They don't like big numbers so you're using a number that's in the general vicinity of a billion billion (1018) What am I missing here?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Using an identity doesn't mean that you have a large number. You could calculate a column of the date + identity if needed.

    Alternatively, why not just calculate the "next" number for that date?

  • I'm confused as well. I'm assuming "they" is your client. But the fact that they don't like big numbers and you are generating a random 50 character number that you are then truncating down to 20 characters confuses me.

    If your client is looking for a faux datetime stamp, there are better ways to do this. This is the one I use.

    Declare @Number char(17);

    SELECT @Number = REPLACE(REPLACE(REPLACE(REPLACE(Convert(char(23),GetDate(),121)

    ,'-',''),':',''),' ',''),'.','');

    SELECT Convert(bigint,@Number);

    --And to see why I replaced your length with 17 and how this works, run the below code.

    SELECT REPLACE(REPLACE(REPLACE(REPLACE(Convert(char(23),GetDate(),121)

    ,'-',''),':',''),' ',''),'.',''), GetDate(),

    LEN(REPLACE(REPLACE(REPLACE(REPLACE(Convert(char(23),GetDate(),121)

    ,'-',''),':',''),' ',''),'.',''))

    Remember that if your client doesn't like big numbers, then this probably isn't a solution either. Because to them, it will look like a big number, despite the fact that this is a datetime value down to the millisecond. If they have a problem, then you need to discuss solutions with them. Not just substitute another big number for the big numbers they're trying to avoid.

    EDIT: You say you're looking for random numbers that are unique. There really is no good way to do non-unique random numbers, though I did write an article about it here[/url]. You will notice, if you read the comments on the article, that even this isn't a truly random number generation system. Even Identity doesn't work as a true measure of uniqueness. The only true measure of unique would be a database-wide GUID and that's a large number.

    Please clarify what you're trying to accomplish with your code (in English, not code) and what your client constraints are that are causing you to jump through hoops. That will help us help you.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

Viewing 13 posts - 1 through 12 (of 12 total)

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