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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:20:43+00:00 2026-05-31T12:20:43+00:00

I have a test function which would sanitize phone nos and allow only nos

  • 0

I have a test function which would sanitize phone nos and allow only nos and characters “x” or “X” to be stored. I have it to where it does most of it other than it allows multiple x’s which I don’t want. Can anybody help me add it to the regular expression also let me know if you spot potential issues ?

CREATE Function [dbo].[RemoveAlphaCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    While PatIndex('%[^0-9,x,X]%', @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex('%[^0-9,x,X]%', @Temp), 1, '')

    Return @TEmp
End
  • 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-31T12:20:44+00:00Added an answer on May 31, 2026 at 12:20 pm

    The problem with PATINDEX here is that it can’t really determine that the pattern should change after it hits a string for the first time. So maybe this approach will be simpler:

    CREATE FUNCTION [dbo].[RemoveAlphaCharacters]
    (
        @Temp VARCHAR(1000)
    )
    RETURNS VARCHAR(1000)
    AS
    BEGIN
        DECLARE @i INT, @hitX BIT, @t VARCHAR(1000), @c CHAR(1);
    
        SELECT @i = 1, @hitX = 0, @t = '';
    
        WHILE @i <= LEN(@Temp)
        BEGIN
            SET @c = SUBSTRING(@Temp, @i, 1);
            IF LOWER(@c) = 'x' AND @hitX = 0
            BEGIN
                SET @t = @t + @c;
                SET @hitX = 1;
            END
            IF @c LIKE '[0-9]'
            BEGIN
                SET @t = @t + @c;
            END 
    
            SET @i = @i + 1;
        END
        RETURN(@t);
    END
    GO
    
    SELECT dbo.RemoveAlphaCharacters('401-867-9092');
    SELECT dbo.RemoveAlphaCharacters('401-867-9092x32');
    SELECT dbo.RemoveAlphaCharacters('401-867-9092x32x54');
    

    Results:

    4018679092
    4018679092x32
    4018679092x3254
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this repeater which makes use of a public function called Test
I have a function which takes in a list of characters and generates the
I would like to create a test function which change the '.' charachter into
I have a function frequencyBy which I would like to parallelize. Here follows a
I have the following function which does a crude job of parsing an XML
I have just come across this code: function test(){ //... if ( $profilerule ==
I have a simple question related to one-line programming. First an example: function test(a)
I have this (simplification): $(li).click(function{alert(test);}); <li> <input> </li> What's the best way to bind
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I have a small sample function: #define VALUE 0 int test(unsigned char x) {

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.