Update bit fields in MSSQL

Source: Internet
Author: User

In MSSQL, bit-type field updates are always performed by first checking and then updating. In this way, you need to query the updates first and then execute the updates.
Suddenly think of whether it is possible to directly update it, each time get the opposite value before the update.
The first thought was to use mid, and later found that MID was not supported in SQL statements.
Data shows: "mid is a PB function. Generally, the database should use the substr function"

Later, I found the "substring" function, which has the same usage as mid, so I have the following method.

Update anketo1 set Fb = substring ('10', FB + 1,1) Where id = 5

Later, I asked BM, and BM said that the reverse request would be taken directly.
Update anketo1 set Fb = ~ FB where id = 5
I told BM to use substring.
BM said: "This guy is amazing. This can also be thought out. This is an evil martial arts ."

Finally, we can use ABS to obtain the absolute value.
Update anketo1 set Fb = ABS (fb-1)
No matter what martial arts, winning is a good martial arts.
The above three methods are all good methods.

 

Appendix:
Substring
Returns a part of a character, binary, text, or image expression. For more information about valid Microsoft & reg; SQL Server & #8482; data types that can be used with this function, see data types.
Syntax
Substring (expression, start, length)
Parameters
Expression

Is a string, binary string, text, image, column, or expression that contains a column. Do not use expressions that contain aggregate functions.

Start
It is an integer that refers to the starting position of the stator string.
Length
It is an integer that refers to the length of the substring (the number of characters to return or the number of bytes ).

 

ABS Functions
Returns the absolute value of a number.

ABS (number)

The number parameter can be any valid numeric expression. If the number contains null, null is returned. If it is a non-initialized variable, 0 is returned.

Description
The absolute value of a number is the unsigned value. For example, ABS (-1) and ABS (1) Both return 1.

The following example uses the ABS function to calculate the absolute value of a number:
Dim mynumber
Mynumber = ABS (50.3) 'returns 50.3.
Mynumber = ABS (-50.3) 'returns 50.3.

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.