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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:00:32+00:00 2026-05-28T18:00:32+00:00

I have a table like this with a computed column: CREATE TABLE PhoneNumbers (

  • 0

I have a table like this with a computed column:

CREATE TABLE PhoneNumbers
(
    [PhoneNumberID] int identity(1,1) not null primary key clustered,
    [Number] varchar(20), /* Entire number, like (800) 555-5000 */
    [Digits] AS dbo.RegExReplace(Number, '[^0-9]', '') PERSISTED  /* Like 8005555000 */
)

It’s created fine, and the Digits column works great as expected, BUT it doesn’t seem to behave as a “PERSISTED” column. When I do a query with Digits in the WHERE clause it’s VERY slow. When I try to add an index to the Digits column I get: Column 'Digits' in table 'PhoneNumbers' is of a type that is invalid for use as a key column in an index.

It seems like that column isn’t really being treated as PERSISTED and is being recomputed on every query and won’t let me add an index.

The RegExReplace is a C# CLR function defined as follows:

[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString RegExReplace(SqlString expression, SqlString pattern, SqlString replace)

Any ideas on how to get that Digits column to act like a persisted column or allow me to add an index?!

Thank you!

  • 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-28T18:00:33+00:00Added an answer on May 28, 2026 at 6:00 pm

    Try a CAST:

    CREATE TABLE PhoneNumbers
    (
        [PhoneNumberID] int identity(1,1) not null primary key clustered,
        [Number] varchar(20), /* Entire number, like (800) 555-5000 */
        [Digits] AS CAST(dbo.RegExReplace(Number, '[^0-9]', '') AS VARCHAR(20)) PERSISTED  /* Like 8005555000 */
    )
    

    I believe that the problem is your CLR function is returning SqlString which ends up being nvarchar(4000) or similar – not indexable.

    It’s kind of a known “problem” with computed columns that the datatype is inferred from the expression. Mainly an issue with strings and “helper functions” which take varchar(max) and also with decimal operations where precision changes due to the calculations.

    I have a little rule where I always CAST – it makes it explicit and avoids any ambiguity. Generally, columns which are known to be small should be explicitly small – varchar(max) seems to have a lot of performance overhead – even if you pass through a function which returns varchar(max) and takes varchar(max), cast back to the size you know, because it will perform a lot better.

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

Sidebar

Related Questions

I have something like this create function Answers_Index(@id int, @questionID int) returns int as
I have a database table with the primary column defined as: ID bigint identity
What I want to do is have a table structure like this: TypeID int
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
I have a table like this: name code group john 12 smith 15 how
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName

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.