Thanks, but not quite what I was looking for. I'd like a scalar function to
convert from a string of a base-2 number (varchar(7) field containing all 0s
and 1s) to an integer

None of these are what I'm looking for:
     select '0111100', cast('0111100' AS INT)
--Result:
      0111100     111100

     select cast('0111100' AS binary(2)), cast(cast('0111100' AS binary(2))
AS int)
--Result:
      0x3031    12337

where the result I'm looking for is 60 (decimal int) (or 0x3C hex), which I
can do a bitwise operation on.

The table I'm selecting from has about 10,000-20,000 records, and I'd like
to select using a bitwise operation on that bitmask field. Seems like there
isn't a SQL function to do this conversion, though.

On the other hand, there's actually less than a dozen distinct combinations
of that bitmask field + some other ID that I'd be checking, so I may just
cache it in CF and store the translations in an array of structures or
something.

Thanks again,
- David

On 7/19/06, Joe <[EMAIL PROTECTED]> wrote:
>
> Hope This helps.
>
> http://msdn2.microsoft.com/en-us/library/ms191530.aspx#_binary
>
> You can convert int, smallint, and tinyint to binary or varbinary, but
> if you convert the binary value back to an integer value, this value
> will be different from the original integer value if truncation has
> occurred. For example, the following SELECT statement shows that the
> integer value 123456 is usually stored as a binary 0x0001e240:
>
> SELECT CAST( 123456 AS BINARY(4) )
>
> However, the following SELECT statement shows that if the binary target
> is too small to hold the entire value, the leading digits are silently
> truncated so that the same number is stored as 0xe240:
>
> SELECT CAST( 123456 AS BINARY(2) )
>
>
>
>
> David wrote:
> > Hi,
> >
> > I've checked the SQL Server references, and haven't found.
> >
> > I have a varchar field, which has a bitmask (7 ones and zeros, like
> 0111100
> > - days of week) and would like in SQL to convert to int.
> >
> > Is there a T-SQL function to do a base-2 conversion of varchar to int?
> >
> > Thanks,
> > - David
> >
> >
> >
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four 
times a year.
http://www.fusionauthority.com/quarterly

Archive: 
http://www.houseoffusion.com/cf_lists/message.cfm/forumid:6/messageid:2512
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:6
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.6

Reply via email to