Bitwise operations of SQL Server

Source: Internet
Author: User

In SQL Server, status fields marked by numbers such as 1, 2, 4, 8, 16... can be accumulated and combined to form various States.

For example, the original number of a record field is 2. If we want to add 4, we can use

Update t_user set iflag = iflag | 4 where userid = 1

(Iflag is the field name)

Example 2: After adding 4, what should we do if we want to remove 4?

Update t_user set iflag = iflag ^ 4 where userid = 1

In this way, 4 is removed from the record.

If we want to select all records for 2, how can we do this?

Select * From t_user where iflag & 2 = 2

Bitwise operations in SQL not only extract various values, but also sort the data.

For example, a field in the News list is marked

1: Stick

2: unstick

4: Recommended

8: Not recommended

The value of this field can be a combination of the four States. What should we do if we want to put all the top states in front of them based on certain conditions?

Select * From t_news order by iflag & 1 DESC

In this way, we will put all the top posts in front. Of course, some where conditions can be added here, and some bit operations can be added in where,

For bit operations, see the corresponding SQL help

Next, let's talk about enumeration bit operations in C #.

Here we define an enumeration

[Flags]
Enum userflag
{
A = 1,
B = 2,
C = 4,
D = 8,
E = 16,
F = 32
}

Add the following processing in the Code:

Protected void page_load (Object sender, eventargs E)
{

If (! Ispostback)
{

String strsql = "select * From v_user where iflag & @ iflag = @ iflag ";

// Sqlparameter parm = new sqlparameter ("@ iflag", sqldbtype. Int, 4 );
// Parm. value = userflag. A | userflag. B;
Sqlconnection con = new sqlconnection ("Server =.; database = sinvan_texdb; user id = sa; Pwd = 123 ;");

Sqlcommand comm = new sqlcommand (strsql, con );

Comm. Parameters. Add ("@ iflag", sqldbtype. Int, 4). value = userflag. A | userflag. B;

Sqldataadapter ADP = new sqldataadapter (Comm );
Datatable dtable = new datatable ();
ADP. Fill (dtable );

Userflag = (userflag) enum. parse (typeof (userflag), dtable. Rows [0] [11]. tostring ());

}
}

After processing, userflag is a combination of various types in the database.

We can also perform bitwise operations on it.

If you want to add userflag. C, perform the following operations:

Userflag = userflag | userflag. c

To remove userflag. C, perform the following operations:

Userflag = userflag ^ userflag. c

If you want to determine whether the ID contains C, perform the following operations:

(Userflag & userflag. c) = userflag. c

Is it similar to the operations in SQL Server, bit operations are common no matter what language, haha

Unfortunately, access does not support bitwise operations.

 

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.