Results 1 to 6 of 6

Thread: storing booleans in SQL Server

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    storing booleans in SQL Server

    I have some fields in a SQL Server that are type bit (0 or 1)
    They are used for true/false.

    When I create an SQL statement to store the boolean values, SQL Server can't convert True into 1 or False into 0.

    Even though if you read in the values from the DB into boolean, it will read correctly.

    What I did was
    Math.Abs(Cint(bVariable)).ToString

    this returns either 1 or 0 for true or false when I am creating my insert statement. The ABS is needed because boolean true translates to -1 when you use cint, so the absolute value is 1 and 0 is always 0.

    is there a better way to do this?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    You can use IIf()

    VB Code:
    1. ... IIf(bValue,1,0) ...

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383
    Originally posted by mendhak
    You can use IIf()

    VB Code:
    1. ... IIf(bValue,1,0) ...
    well which do you think is better to use?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    I say you should use IIf(), because it fits your requirement perfectly. What you're using is something like a workaround, which does work, but wasn't made for this purpose. Know what I mean?

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383
    yeah i just remember people used to complain about the IIF because it evaluates both the true part and false part regardless of the outcome, but i suppose in this situation that doesnt matter since the true and false parts are both just integers

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    Then make your own If...Then...Else function using AndAlso.

Posting Permissions

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



Click Here to Expand Forum to Full Width