Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:18:29+00:00 2026-06-05T02:18:29+00:00

I have a table with a column of bit values. I want to write

  • 0

I have a table with a column of bit values. I want to write a function that returns true if all records of an associated item are true.

One way I found of doing it is:

Select @Ret = CAST(MIN(CAST(IsCapped as tinyInt)) As Bit) 
    from ContractCover cc
    Inner join  ContractRiskVersion crv on cc.ContractRiskId = crv.ContractRiskId
    WHERE crv.ContractVersionId = @ContractVersionId
    AND cc.IsActive = 1
    return @ret

But is the casting to int to get the minimum expensive? Should I instead just be querying based on say:

(count(Id) where IsCapped = 0 > 0) returning false rather than doing the multiple casts?

In the execution plan it doesn’t seem like calling this function is heavy in the execution (but I’m not too familiar with analysing query plans – it just seems to have the same % cost as another section of the stored proc of like 2%).

Edit – when I execute the stored proc which calls the function and look at the execution plan – the part where it calls the function has a query cost (relative to the batch) : 1% which is comparable to other sections of the stored proc. Unless I’m looking at the wrong thing 🙂

Thanks!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:18:32+00:00Added an answer on June 5, 2026 at 2:18 am

    I would do this with an exists statement as it will jump out of the query from the moment it finds 1 record where IsCapped = 0 where as your query will always read all data.

    CREATE FUNCTION dbo.fn_are_contracts_capped(@ContractVersionId int)
    RETURNS bit
    WITH SCHEMABINDING
    AS
    BEGIN
        DECLARE @return_value bit
    
        IF EXISTS(
          SELECT 1 
            FROM dbo.ContractCover cc
            JOIN dbo.ContractRiskVersion crv 
              ON cc.ContractRiskId = crv.ContractRiskId
           WHERE crv.ContractVersionId = @ContractVersionId
             AND cc.IsActive = 1
             AND IsCapped = 0)
          BEGIN
            SET @return_value = 0
          END
        ELSE
          BEGIN
            SET @return_value = 1
          END
    
        RETURN @return_value
      END
    

    Compared to the IO required to read the data, the cast will not add a lot of overhead.

    Edit: wrapped code in a scalar function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table that has a bit column named Active .
I have table with date/time column and person column. Each person has multiple records
I have a table that contains records that can become part of a bill.
I have a column in my table which contains the values 0 or 1.
I have a table of product information with many bit columns. This table can
I have a table [Users] with the following columns: INT SmallDateTime Bit Bit [UserId],
I have a table column I’m trying to expand and hide. jQuery seems to
I have a table column called post_tags within a table called posts where assigned
I have a table column where each row has one of three states, and
If I have a table column with data and create an index on this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.