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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:36:10+00:00 2026-06-09T00:36:10+00:00

I want to convert a days-since-last-activity value in my SQL Server stored procedure to

  • 0

I want to convert a days-since-last-activity value in my SQL Server stored procedure to a logarithmic representation for weighted ordering (alongside other criteria) of the matching search results before truncating them. Log2 approximates my needs pretty well and seems like something that should be very efficient.

CAST (LOG(lastActivityAge)/LOG(2)) AS TINYINT

Would work fine, but it seems extremely wasteful to ask my server to do such heavy lifting just to get the highest set bit position.

Any suggestions on a more efficient way to do this?

  • 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-09T00:36:12+00:00Added an answer on June 9, 2026 at 12:36 am

    There are some bit twiddling hacks to find the highest bit but they cannot be expressed easily in T-SQL (requiring loops). UDF’s are out of questions for performance reasons.

    You can go a little bit faster with Denali:

    CAST (LOG(lastActivityAge, 2)) AS TINYINT
    

    Or hard-code the LOG(2) expression as I’m not sure the optimizer can constant-fold it away. Anyway, you need millions of rows to notice the difference.

    One last trick: If there are some very common values, try this:

    case lastActivityAge when 1 then 0 when 2 then 1 /* ... */ else CAST (LOG(lastActivityAge, 2)) AS TINYINT end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a pointer *int to its real value int , in
I want to get a total days between 2 dates Query Select DATEDIFF(DAY, CONVERT(DATETIME,
I have a MySql datetime value like 2012-04-17 20:48:29. I want to convert this
I want to convert a string to formatted text in C# in WPF application,
I want to convert all the URLs in a javascript string to links, in
I want to convert a column of numbers to numeric, but there are certain
I want to convert a number to hex and store the result in a
I want to convert plaintext to link for an input I have. The HTML
I want to convert XML into binary data in Java? What is the fastest
I want to convert sqlite query to Ormlite query. SELECT * FROM Test where

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.