Postgresql Convert bit varying to integer - postgresql

Searched the postgresql docs http://www.postgresql.org/docs/8.4/interactive/functions-bitstring.html for information on converting bit varying to integer
But couldnt' find any info.
select '011111'::bit(4)::varbit(4)::integer as varbit
Appreciate your response.

One way:
SELECT b, lpad(b::text, 32, '0')::bit(32)::int
FROM (
VALUES
('01'::varbit)
,('011111')
,('111')
) t (b);
Result:
b | lpad
-------+------
01 | 1
011111 | 31
111 | 7
Related answer:
Convert hex in text representation to decimal number

Let's say our table has 3 columns and 3 rows. We can simulate it with:
select *
from (
values ('row1', 1::int, 12::bit(8)::varbit),
('row2', 2::int, 23::bit(8)::varbit),
('row3', 3::int, 34::bit(8)::varbit)
) as T(A,B,C);
As you can see, first columns is varchar , second is int and the third is varbit.
Let's convert third column to int:
select C::bit(8)::int
from (
values ('row1', 1::int, 12::bit(8)::varbit),
('row2', 2::int, 23::bit(8)::varbit),
('row3', 3::int, 34::bit(8)::varbit)
) as T(A,B,C);
==C==
12
23
34
The point is, you have to convert it to bit(n) first, then you can convert bit(n) to varbit. The same thing is also true for int to varbit.

Related

Extract 2 digit from a BigInt of 8 digit in Hive

In my hive table I need to extract from column DATE(stored as bigint), 2 digits(month) in center.
For Example:
20220120 -> 01
20211201 -> 12
Is it possible to do directly without casting column DATE to string?
Thanks
substr should work.
select substr( 20220101,4,2)

How to add a leading zero based on range of values in postgres within an integer column

New to postgres and unsure how to accomplish the following. I have a table as follows:
create table if not exists my_table (
id int GENERATED BY DEFAULT AS IDENTITY primary key,
key int default 0
)
What I am trying to do is take an integer value (my_key) and if it's >= 0 and < 10 then add a leading zero (0) to it and insert it into my_table.key
I have tried to_char(my_key::integer,'09')::integer where my_key = 0 and it doesn't insert 00 within the key column.
Any help would be great.
Leading zeros don't change the value of an integer, so this is a question about formatting numbers.
If you want to display the id column with leading zeros, you could do that like this:
SELECT to_char(id, '00'), key
FROM "table";
The format 00 formats the number as a two-digit string with leasing zeros. If id is greater than 99, the number cannot be formatted like this, and you will get ##.
See the documentation for details about to_char and the available formats.

Padding Zero to left in Varchar

I have a column tattoo that is varchar(20) type. I want this to be padded left with 3 zeros. N009 should become 000N009.
to_char(integer/double precsion/int/timestamp,text) but is there any way we could do this to varchar?
Casting varchar(20) to integer did not worked either. Some of my code added below:
select tattoo,to_char(tattoo,'0000FM')::varchar as tm from animals where soc_code = 'AURO' limit 100
select tattoo,to_char(tattoo::integer,'0000FM')::varchar as tm from animals where soc_code = 'AURO' limit 100
select tattoo,to_char(cast(tattoo as integer),'0000FM')::varchar as tm from animals where soc_code = 'AURO' limit 100
They both throw errors saying invalid input syntax for integer and function_to_char does not exist.
have you tried LPAD()
select LPAD( tattoo, 7, '0' )
I want this to be padded left with 3 zeros. N009 should become 000N009.
You may be overthinking this. Just concatenate:
SELECT '000' || tatoo;
If tatoo is NULL, so is the result.

Trying to get rid of unwanted records in query

I have the following query
Select * from Common.dbo.Zip4Lookup where
zipcode='76033' and
StreetName='PO BOX' and
'704' between AddressLow and AddressHigh and
(OddEven='B' or OddEven = 'E')
The AddressLow and AddressHigh columns are varchar(10) fields.
The records returned are
AddressLow AddressHigh
------------ ------------
1 79
701 711
The second is the desired record How do I get rid of the first record.
The problem is that SQL is using a string compare instead of a numeric compare. This is because AddressLow/High are varchar and not int.
As long as AddressLow/High contain numbers, this should work:
Select * from Common.dbo.Zip4Lookup where
zipcode='76033' and
StreetName='PO BOX' and
704 between
CAST(AddressLow as INT) and
CAST(AddressHigh as INT) and
(OddEven='B' or OddEven = 'E')
The problem is that your condition fits to the first record in 7 on the beginning of the 79 because it's the string value. The easist way is IMHO change the data type to some numeric one.

Function in Postgres to convert a varchar to a big integer

I have a varchar column in Postgres 8.3 that holds values like: '0100011101111000'
I need a function that would consider that string to be a number in base 2 and spits out the numeric in base 10. Makes sense?
So, for instance:
'000001' -> 1.0
'000010' -> 2.0
'000011' -> 3.0
Thanks!
Cast to a bit string then to an integer.
An example:
'1110'::bit(4)::integer -> 14
Though you had varying length examples, and were after bigint, so instead use bit(64) and pad the input with zeroes using the lpad function.
lpad('0100011101111000',64,'0')::bit(64)::bigint
Here's a complete example...
create temp table examples (val varchar(64));
insert into examples values('0100011101111000');
insert into examples values('000001');
insert into examples values('000010');
insert into examples values('000011');
select val,lpad(val,64,'0')::bit(64)::bigint as result from examples;
The result of the select is:
val | result
------------------+--------
0100011101111000 | 18296
000001 | 1
000010 | 2
000011 | 3
(4 rows)