convert -ve int to +ve bigint

  • Hi,

    How do you convert the -ve int value to +ve bigint value.

    I have used cast and convert which didn't help.

    Example.

    declare @bigIntCard bigint

    declare @Card int

    Set @Card = -199870816

    Set @bigIntCardField1 = cast(@Card as bigint)

    print @bigIntCardField1

    I expected it to be casted as a +ve bigint value.

    Is there a way to do it? Please help me.

    Thanks

  • How about:

    Set @bigIntCardField1 = @Card * -1

    SQL server does an implicit conversion from int to bigint so no need to cast @Card

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • I believe the int gets to a max of 2147483648 before it flips over to negatives, and it then starts off at -2,147,483,648 and counts down towards 0, as 2^32 is represented as -1.

    So I think you would need to do

    Bigint value =2147483648+ABS(-2,147,483,648)+Value.

    so I beleive this is the code (needs testing though).

    declare @bigIntCard bigint

    declare @Card int

    Set @Card = -199870816

    Set @bigIntCard = Cast(2147483648 as bigint)+((Cast(2147483648 as bigint))-@Card)

    print @bigIntCard

    EDIT : Maths was wrong and corrected. Not sure if you need to add one.

    If you plug -1 into this you get the number 4294967298 which is 2^32.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • The +ve value should be 2305969845.

    It is stored as a -ve number in the column when the datatype is int. But in one stored proc I need to convert it into a bigint which should be converted into an +ve number

  • How did you get to this number 2305969845 as I dont get close even by just adding the abs(-199870816) to the top value for an Integer.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Sorry by INT Max value wrong it should read

    declare @bigIntCard bigint

    declare @Card int

    Set @Card = -199870816

    Set @bigIntCard = Cast(2147483647 as bigint)+((Cast(2147483648 as bigint))+@Card)+1

    print @bigIntCard

    You also need the +1, otherwise it doesnt work.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • (CAST(@Card AS BIGINT) & 4294967295) should do the job for any card value

  • Thank you guys.

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

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