>- see footer for list info -<
Binary Strings?

What like "1001"?

Or are we talking Binary Strings

0x09 (same value as above)

If its the former you're in for a real treat cos there is no simple way, you're 
looking at a user function to convert it to a decimal or hex equivalent then 
you can use the conventional funcs.

I can't remember why we do this, but we do, so here are the bits you'll need

The FUNC -----

CREATE  FUNCTION char_binary_to_int 
        (@bin_str varchar(50))
RETURNS int
AS
BEGIN
        DECLARE @pos as int
    DECLARE @val as int
    DECLARE @idx as int
    DECLARE @posc as char(1)

    SET @idx = 1
    SET @pos = Len(@bin_str)
    SET @val = 0

    WHILE @pos >= 0
    BEGIN
        SET @posc = SubString(@bin_str,@pos,1)
        If (@posc = '1')
        BEGIN
            SET @val = @val + @idx
        END
        SET @pos = @pos -1
        SET @idx = @idx * 2
        
    END

    RETURN @val
END


---------------

The Test Code ------


declare @t_bin as binary(4)
declare @t_str as char(8)
declare @t_int as int

SET @t_str = '10010001'
set @t_int = dbo.char_binary_to_int(@t_str)
set @t_bin = CAST(@t_int as binary(4))


PRINT @t_str
PRINT @t_int
PRINT @t_bin
PRINT CAST(255 as binary(4))

If ((@t_int & 128) > 0)
BEGIN
    PRINT 'Bitmask match'
END

--------------------

output will look like this

10010001
145
0x00000091
0x000000FF
Bitmask match




regards

Steve


Tom Shearer (lists) <[EMAIL PROTECTED]> wrote :

> >- see footer for list info -
> Hi All,
> 
> Was wondering if anyone had any handy knowledge of how I might convert a
> binary string to an int in mssql.
> 
> For example I have:
> 
> 
> 0x713
> 
> which would translate to:
> 
> 000001*1*100010011
> 
> 
> 
> (not worried about padding right now ;) )
> 
> 
> 
> I have a cf function that does this, but I need to query an mssql
> table to find out if a flag is set - running 4+ million data rows
> through cf to find out if the flag is set is not ideal - thankfully I
> only need to do this on a months worth of data.
> 
> 
> 
> Any help is appreciated - I'm sick of google ;)
> 
> 
> 
> TIA,
> 
> 
> 
> Tom
> _______________________________________________
> 
> For details on ALL mailing lists and for joining or leaving lists, go to 
> http://list.cfdeveloper.co.uk/mailman/listinfo
> 
> --
> CFDeveloper Sponsors:-
> >- cfdeveloper Hosting provided by www.cfmxhosting.co.uk -
> >- Lists hosted by www.Gradwell.com -
> >- CFdeveloper is run by Russ Michaels, feel free to volunteer your help -
Steve Powell
[EMAIL PROTECTED]
07971 583792




_______________________________________________

For details on ALL mailing lists and for joining or leaving lists, go to 
http://list.cfdeveloper.co.uk/mailman/listinfo

--
CFDeveloper Sponsors:-
>- cfdeveloper Hosting provided by www.cfmxhosting.co.uk -<
>- Lists hosted by www.Gradwell.com -<
>- CFdeveloper is run by Russ Michaels, feel free to volunteer your help -<

Reply via email to