Solution to the problem of bigint int with symbolic times error in SQL Server _mssql

Source: Internet
Author: User

One requirement is to store multiple states in a cloud-monitored state value (including various exceptions, warning states that can exist simultaneously) using a bitwise operation mechanism to store in an int.

Now the monitoring log data is very large (billion levels) need to data per hour, daily aggregation for online report use.
The status is divided into 3 levels: normal (0), Warning (1), exception (2), aggregation needs to use the max to choose the worst state, you need to process the state value and the number of States, it is necessary to use the bigint type to do the operation,

The problem is when you get the original state value when you convert bigint to int, SQL Server complains:

Message 8115, Level 16, State 2, line 1th
An arithmetic overflow error occurred while converting expression to a data type int.

Because the 0x80000000 is already used in the status code, there is a problem with the sign bit.
A conversion function was written to solve the problem.

CREATE FUNCTION [dbo]. [Biginttoint]

(

@Value bigint 

)

RETURNS int

as

BEGIN

--Is there an int symbol bit

if @Value & 0x80000000 <> 0 Return @Value & 0xFFFFFFFF | 0xffffffff00000000

--unsigned bit return

@Value & 0xFFFFFFFF End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.