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 4097192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:08:32+00:00 2026-05-20T20:08:32+00:00

I have a very typical situation. We have a table called Users which has

  • 0

I have a very typical situation. We have a table called Users which has a column called Branches (varchar 1000).

The organization can have 1000 branches. So if a user has access to branch 1, 5, and 10, the branches string would look like:

1000100001000000000……

(i.e. 1 for a position a User has branch access to based on the branch’s number). Please do not advise better data storage options, this is coming to me from a legacy application that is deployed across continents.

Now given this background (and considering that there can be > 10000 users), I want to search for all Users who have access to any one of given set of branches, e.g. Find all users who have access to either branch 10, 65, 90 or 125.

One easy solution is to convert the desired set of branches (i.e. 10, 65, 90, 125) to a branch string (00000010100 etc), then use a scalar UDF to iterate over both the branch strings and return true at first matching occurence where 2 branch strings have 1, and false if there is not a 1 at common position.

Other than that, I also have an option of searching in application in C#. Some of these users are privileged (approx 1000 or more) and their data is cached in application as it is accessed very frequently. But for other users that are not privileged, data is only in db.

I have 2 questions here:
1) For a db search, is there a better way other than the UDF approach I mentioned.
2) For privileged users, what would be better in terms of performance, search in application (which further can be based on a for loop on branch strings like in UDF, or as a Linq Intersect operator on 2 branch arrays, i.e. a Linq Intersect on [1,5,9,50,80,200] and [6,90,256,300] etc.)
Would a db search produce faster results or an application based search?

Consider there might be other parameters for search in both cases, e.g. Last name starts with.

My current approach is to filter rows in db for both situations first on other parameters (like Last name starts with). Then use a scalar UDF to filter this result-set based on branches and then return the results.

  • 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-05-20T20:08:33+00:00Added an answer on May 20, 2026 at 8:08 pm

    Do it in SQL, it will be only 100 times faster than doing it in C# or other front end.

    Use the built-in numbers table to break the long string into positions (number series goes up to 2047).

    Sample tables

    create table users (userid int)
    insert users select 1 union all select 2
    
    create table permission (userid int, bigstr varchar(1000))
    insert permission
    select 1, REPLICATE('0', 56) + '1' -- 57th
            + REPLICATE('0', 32) + '1' -- 90th
            + REPLICATE('0', 64) + '1' -- 155th
            + REPLICATE('0', 845)
    insert permission
    select 2, REPLICATE('0', 66) + '1' -- 67th
            + REPLICATE('0', 98) + '1' -- 166th
            + REPLICATE('0', 657) + '1' -- 824th
            + REPLICATE('0', 176)
    

    Sample showing all the matching permissions against a list

    select *
    from users u
    inner join permission p on p.userid=u.userid
    inner join master..spt_values v on v.type='p'
      and SUBSTRING(p.bigstr,v.number,1) = '1'
      and v.number between 1 and LEN(p.bigstr)  -- or 1000 if it is always 1000
    where v.number in (57,90,824)
    

    To find users who have at access to at least one branch in the list:

    select distinct u.userid
    from users u
    inner join permission p on p.userid=u.userid
    inner join master..spt_values v on v.type='p'
      and SUBSTRING(p.bigstr,v.number,1) = '1'
      and v.number between 1 and LEN(p.bigstr)  -- or 1000 if it is always 1000
    where v.number in (57,90,824)
    

    etc..

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

Sidebar

Related Questions

I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very simple problem which requires a very quick and simple solution
I have a very simple WPF application in which I am using data binding
I have a very common situation and a solution, but I would like to
I have a typical search facility in my app which returns a list of
We have very strange problem, one of our applications is continually querying server by
I have very little experience building software for Windows, and zero experience using the
i have very simple problem. I need to create model, that represent element of
I have a very strange problem. Under some elusive circumstances I fail to apply
I have a very large code base that contains extensive unit tests (using CppUnit).

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.