Skip to main content
added 320 characters in body
Source Link
Vérace
  • 30.6k
  • 9
  • 71
  • 84

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For the number 43, just substitute your column of choice. It's probably possible to do this using recursive CTEs, but that's a bit aboutabove my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle herehere. The more observant among you will notice that that code does not remove leading zeros - the code above has to be encase (entombed?) in this snippet

select replace(ltrim(replace(ColumnName,'0',' ')),' ','0')

which can be found here.

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here.

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For the number 43, just substitute your column of choice. It's probably possible to do this using recursive CTEs, but that's a bit above my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here. The more observant among you will notice that that code does not remove leading zeros - the code above has to be encase (entombed?) in this snippet

select replace(ltrim(replace(ColumnName,'0',' ')),' ','0')

which can be found here.

Missing fiddle!
Source Link
Vérace
  • 30.6k
  • 9
  • 71
  • 84

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle herehere. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here.

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here.

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here.

added 901 characters in body
Source Link
Vérace
  • 30.6k
  • 9
  • 71
  • 84

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here.

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

No need to use a function - you can do it in (Oracle's) SQL.

SELECT LISTAGG(SIGN(BITAND(43, POWER(2,LEVEL-1))),'') 
       WITHIN GROUP(ORDER BY LEVEL DESC) bin
FROM dual
CONNECT BY POWER(2, LEVEL-1)<=43;

Result:

BIN
101011

I found this deadly snippet here and the fiddle is here. For 43, just substitute your column. It's probably possible to do this using recursive CTEs, but that's a bit about my pay grade :-).

And to reverse the process, you can use this snippet

WITH INPUT AS
(SELECT REVERSE('1000') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);

Result:

OUTPUT
8

From here - dbfiddle here. Again, a recursive CTE might do the trick. Again, for '1000', just substitute your column.

Just for kicks, I found another function which will work (with tweaks) on older versions of databases that don't have recursive CTEs. From the excellent Orafaq site here.

SELECT 
  DECODE(BITAND(VALUE, 128), 128, '1', '0') ||
  DECODE(BITAND(VALUE, 64), 64, '1', '0') ||
  DECODE(BITAND(VALUE, 32), 32, '1', '0') ||
  DECODE(BITAND(VALUE, 16), 16, '1', '0') ||
  DECODE(BITAND(VALUE, 8), 8, '1', '0') ||
  DECODE(BITAND(VALUE, 4), 4, '1', '0') ||
  DECODE(BITAND(VALUE, 2), 2, '1', '0') ||
  DECODE(BITAND(VALUE, 1), 1, '1', '0') as bin_number from 
(select 8 as value from dual) A;

Result:

MY_BIN
1000

Fiddle here.

Source Link
Vérace
  • 30.6k
  • 9
  • 71
  • 84
Loading