BIT shifting with AND operation in SQL select - sql

I have a bit shifting to be done on NUMBER column in table.
It is something like (NUMBER & MASK) >> 27 .. and i need to compare the result of this. Is this something that can be done through SQL select query. So far i am using scripts to do this/

Oracle's PL/SQL has a BITAND() function. Shift-right is the same as integer division by a power of 2. Assuming X is an integer, X >> 27 is the same as X / 134217728.

Related

SQL function to transform number with a certain pattern

I need for a SQL query to transform an int with a value between 1 to 300000 to a number which has this pattern : always 8 number.
For example:
1 becomes 00000001,
123 becomes 00000123,
123456 becomes 00123456.
I have no idea how to do that... How can I do it?
In Standard SQL, you can use this trick:
select substring(cast( (num + 100000000) as varchar(255)) from 2)
Few databases actually support this syntax. Any given database can do what you want, but the method depends on the database you are using.
For MS SQL Server
You could use FORMAT function, like this:
SELECT FORMAT(123,'00000000')
https://database.guide/how-to-format-numbers-in-sql-server/#:~:text=Starting%20from%20SQL%20Server%202012,the%20output%20should%20be%20formatted.
Read at the link Leading Zeroes
For MySql/Oracle
You could use LPAD, like this:
SELECT LPAD('123',8,'0')
https://database.guide/how-to-add-leading-zeros-to-a-number-in-mysql/

How to perform bitwise operations on a value stored in a table and use it in where clause of sql query?

This is the scenario. I have a column 'flag' in a table. This column stores a 32bit binary number in decimal form. For example 00000000000000000000000000000010 is stored as 2. I need to display all the records in the table in which the 26th bit of the value stored in 'flag' is set as '1'. How do I write sql query for this purpose?
Try this:
select * from tablename
where (flag & 64) = 64
The 26th bit corresponds to bit for 2 ^ 6 = 64. On SQL Server, & will do a bitwise-AND and return 64 only if the 64 bit is set in both operands.

SQL Server POWER function

Using SQL Server 2008 R2 when I enter the following query:
SELECT CAST(POWER(2.0, 63.0) AS BIGINT);
Which yields the result:
9223372036854775800
However, using the Windows desktop calculator and raising 2 to the 63 yields:
9223372036854775807
Can someone please explain the difference -- or is there some internal conversion that SQL Server is doing? ... or am I missing something else?
The range of BIGINTin MS Sql Server is:
-2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
And your calculator is giving you the wrong number, because 2^63 can't have an odd number for its right-most digit.
The POWER function in SQL Server (http://technet.microsoft.com/en-us/library/ms174276.aspx), returns the same type as its first argument.
The correct way to write this query is:
DECLARE #foo REAL = 2.0
SELECT CAST(POWER( #foo, 63.0 ) AS BIGINT)
By which, you will get Arithmetic overflow error converting expression to data type bigint. error message.
And about the reason that's
http://www.extremeoptimization.com/resources/Articles/FPDotNetConceptsAndFormats.aspx
And regarding the question of why POWER function is returning a wrong number? As #simonatrcl mentioned in his answer, there is arithmetic problems with floating-point numbers which sometimes result in invalid result. You can read about floating-point numbers and the problems with them here:
http://www.extremeoptimization.com/resources/Articles/FPDotNetConceptsAndFormats.aspx
You can also check the boundaries for integer types in MS Sql Server here:
http://technet.microsoft.com/en-us/library/ms187745.aspx
Power will be returning a FLOAT. Floating point numbers are not accurate beyond certain limits, and will drop a bit of accuracy (if you've ever has a negative 0 problem you'll know what I mean!).
That's what you're getting here...
As far as the calculator goes and tested on XP, Win7 and Win8.1:
2^63 = 9223372036854775808 (obviously)
As far as MSSQL goes:
The upper limit of a BIGINT is defined as 2^63-1, meaning 1 less than 2^63
Now if you would like MSSQL to calculate that for you one would be tempted to write something like:
SELECT POWER(CAST(2 AS BIGINT), 63) - 1
The result would be a bigint because you've cast the first argument of the power to a bigint. MSSQL will first calculate the power and then subtract 1. However, since the result of the power would exceed the range of a bigint, this statement will fail: Arithmetic overflow error converting expression to data type bigint.
So let us invoke some math to solve this. I assume everyone agrees with
2^4 = 2 * 2 * 2 * 2 = 2 * (2^3) = 2^3 + 2^3
and thus
2^4-1 = 2 * 2 * 2 * 2 - 1 = 2 * (2^3) - 1 = 2^3 + 2^3 - 1
That's what we're going to make use of...
SELECT POWER(CAST(2 AS BIGINT), 62) + (POWER(CAST(2 AS BIGINT), 62) - 1)
This results in 9223372036854775807 which is indeed the upper limit of a bigint.
Note that the () around the subtraction is really needed. Otherwise the addition of the result of the two powers would be done first, again resulting in an overflow.

SQL update to a table based on a flag word?

I've got a field in my DB that's an arbitrary value on a per-row basis, and I'd like to add X to this. I'd only like to add X if a flag word (held as an int in this row) has the 2nd and 10th bits set true. Is it possible to create an SQL statement to do this for every row in the table? Or do I have to iterate through my entire table?
Using MySQL (5.5)
Bonus points question: I say add X based on a flag, but there's also a scaling factor. For example, based on a value of bits 20-12 interpreted as a short unsigned integer, I'd really like to assign:
value = value + ('X' * thatShort * (bit2 and bit10));
In MS SQL:
update MyTable
set Field1 = Field1 + 'X'
where Field2 & 0x202 = 0x202
[EDIT]
value = value (X * (field & 0x1FF800 >> 12) * 0x202)
0x1FF800 - is the mask from 12 to 20.
>> 11 - shift it to remove bits from 0 to 12.
since you are filtering by bit2 and bit10 set, then (bit2 and bit10) = 0x202
Hope this will answer your question. Not sure how you are going to grant 'bonus points' though :).

SQL for extract portion of a string

I have a zipcode stored in a text field (string) and would like to select only the last 3 digits of the value in my select statement. is this possible? Is there a standard way of doing this so that the SQL is interchangeable accross databases? I will be using it in production on Oracle, but i test on Interbase (yes, yes, i know, two totally diff DBs, but thats what i am doing)
thanks for any help you can offer
Assuming the zipcodes all have the same length, you can use substr.
If they don't have the same length, you have to do similar things with the strlen function.
Interbase does not have a built-in substring function, but it does have a UDF (user defined function) called SUBSTR in lib_udf.dll that works like this:
select substr(clients.lastname, 1, 10)
from clients
You declare the UDF like this:
DECLARE EXTERNAL FUNCTION SUBSTR
CSTRING(80),
SMALLINT,
SMALLINT
RETURNS CSTRING(80) FREE_IT
ENTRY_POINT 'IB_UDF_substr' MODULE_NAME 'ib_udf';
Oracle does have a built-in substr function that you use like this:
select substr(clients.lastname, 1, 10)
from clients
--jeroen
This depends on how your storing the zip code. If you are using 5 digits only
then this should work for Oracle and may work for Interbase.
select * from table where substr(zip,3,3) = '014'
IF you store Zip + 4 and you want the last 3 digits and some are 5 digits and some are 9 digits you would have to do the following.
select * from table where substr(zip,length(zip) -2,3) = '014'
and one option that may work better in both databases is
select * from table where zip like '%014'