sql >> Database teknologi >  >> RDS >> Sqlserver

SQL Server Konverter heltal til binær streng

Faktisk er dette VIRKELIG ENKELT ved at bruge almindelig gammel SQL. Brug blot bitvise AND'er. Jeg var en smule forbløffet over, at der ikke var en simpel løsning på nettet (som ikke involverede UDF'er). I mit tilfælde ville jeg virkelig tjekke, om bits var slået til eller fra (dataene kommer fra dotnet eNums).

Derfor er her et eksempel, der vil give dig separat og sammen - bitværdier og binær streng (den store union er bare en hacky måde at producere tal på, der fungerer på tværs af DB'er:

    select t.Number
    , cast(t.Number & 64 as bit) as bit7
    , cast(t.Number & 32 as bit) as bit6
    , cast(t.Number & 16 as bit) as bit5
    , cast(t.Number & 8 as bit) as bit4
    , cast(t.Number & 4 as bit) as bit3
    , cast(t.Number & 2 as bit)  as bit2
    ,cast(t.Number & 1 as bit) as bit1

    , cast(cast(t.Number & 64 as bit) as CHAR(1)) 
    +cast( cast(t.Number & 32 as bit) as CHAR(1))
    +cast( cast(t.Number & 16 as bit)  as CHAR(1))
    +cast( cast(t.Number & 8 as bit)  as CHAR(1))
    +cast( cast(t.Number & 4 as bit)  as CHAR(1))
    +cast( cast(t.Number & 2 as bit)   as CHAR(1))
    +cast(cast(t.Number & 1 as bit)  as CHAR(1)) as binary_string
    --to explicitly answer the question, on MSSQL without using REGEXP (which would make it simple)
    ,SUBSTRING(cast(cast(t.Number & 64 as bit) as CHAR(1)) 
                    +cast( cast(t.Number & 32 as bit) as CHAR(1))
                    +cast( cast(t.Number & 16 as bit)  as CHAR(1))
                    +cast( cast(t.Number & 8 as bit)  as CHAR(1))
                    +cast( cast(t.Number & 4 as bit)  as CHAR(1))
                    +cast( cast(t.Number & 2 as bit)   as CHAR(1))
                    +cast(cast(t.Number & 1 as bit)  as CHAR(1))
                    ,
                    PATINDEX('%1%', cast(cast(t.Number & 64 as bit) as CHAR(1)) 
                                        +cast( cast(t.Number & 32 as bit) as CHAR(1))
                                        +cast( cast(t.Number & 16 as bit)  as CHAR(1))
                                        +cast( cast(t.Number & 8 as bit)  as CHAR(1))
                                        +cast( cast(t.Number & 4 as bit)  as CHAR(1))
                                        +cast( cast(t.Number & 2 as bit)   as CHAR(1))
                                        +cast(cast(t.Number & 1 as bit)  as CHAR(1)  )
                    )
,99)


from (select 1 as Number union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 
    union all select 7 union all select 8 union all select 9 union all select 10) as t
 

Frembringer dette resultat:

num bit7 bit6 bit5 bit4 bit3 bit2 bit1 binary_string binary_string_trimmed 1 0 0 0 0 0 0 1 0000001 1 2 0 0 0 0 0 1 0 0000010 10 3 0 0 0 0 0 1 1 0000011 11 4 0 0 0 1 0 0 0 0000100 100 5 0 0 0 0 1 0 1 0000101 101 6 0 0 0 0 1 1 0 0000110 110 7 0 0 0 0 1 1 1 0000111 111 8 0 0 0 1 0 0 0 0001000 1000 9 0 0 0 1 0 0 1 0001001 1001 10 0 0 0 1 0 1 0 0001010 1010

  1. MariaDB JSON_OBJECT() Forklaret

  2. Top fem softwareapplikationer til at få adgang til MySQL/MariaDB-servere

  3. Sådan omdøbes en MySQL-database

  4. Hvordan ROWNUM fungerer i pagineringsforespørgsel?