boolean variant and SQL bit

Results 1 to 2 of 2

Thread: boolean variant and SQL bit

  1. #1
    Join Date
    Dec 1969

    Default boolean variant and SQL bit

    (NOTE: I solved the problem, as shown below, but maybe there is a better solution)<BR><BR>I&#039;m using a boolean variable (b_cookies) in my script. I&#039;m trying to write it to an MS SQL database field of type bit.<BR><BR>I&#039;m getting an error:<BR><BR>Error Type:<BR>Microsoft OLE DB Provider for ODBC Drivers (0x80040E07)<BR>[Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to bit.<BR><BR>Should I assume that boolean variants need to be converted to "1" or "0" manually before they can sent to stored procedures?<BR><BR>I tried cbool(b_cookies) and got an error.<BR><BR>this worked:<BR><BR>if b_cookies then b_cookies=1 else b_cookies=0 end if<BR><BR>Anyway, I don&#039;t actually need an answer, but I&#039;d be interested to know if this the standard operating procedure for this problem.

  2. #2
    Join Date
    Dec 1969

    Default The root of the problem... that the VariantChangeTypeEx code used by VBS to convert from boolean to integer of any kind (including bit) always convers true to a -1. That is, 0xFFFFFFFF. <BR><BR>And most integer types are perfectly happy getting -1 and then cutting it down as need be to fit the DB field size.<BR><BR>But not SQL Server and the "bit" type. It hiccups because it can&#039;t handle anything but 1 and 0, and -1 just doesn&#039;t look like a 1 to it.<BR><BR>What you *could* do:<BR><BR>b_cookies = CLNG(b_cookies) AND 1<BR><BR>The CLNG forces the conversion of true/false to 0xFFFFFFFF and 0x00000000. Then the AND operator does a *bit wise* anding of that value with 0x00000001 and the result is...ta da!....1 or 0!<BR><BR>But what the heck, your way works and is a *LOT* clearer.<BR><BR>p.s.: You don&#039;t need the "end if" if you do that all on one line.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts