|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 04, 2013 10:54 AM
Points: 4,
Visits: 13
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Yesterday @ 10:34 AM
Points: 494,
Visits: 2,158
|
|
How about:
Set @bigIntCardField1 = @Card * -1 SQL server does an implicit conversion from int to bigint so no need to cast @Card
----------------------------------- http://www.SQL4n00bs.com
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:30 AM
Points: 803,
Visits: 2,124
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 04, 2013 10:54 AM
Points: 4,
Visits: 13
|
|
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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:30 AM
Points: 803,
Visits: 2,124
|
|
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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:30 AM
Points: 803,
Visits: 2,124
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 11:34 PM
Points: 47,
Visits: 266
|
|
| (CAST(@Card AS BIGINT) & 4294967295) should do the job for any card value
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 04, 2013 10:54 AM
Points: 4,
Visits: 13
|
|
|
|
|