remove values from string

Shambhu Rai 1,411 Reputation points
2022-08-17T10:50:23.187+00:00

Hi Expert,

how will i remove all the characters from values in sql

Create table test1
(id string, details string)

insert into test1
values('20kjk','3ddf')

Regards

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,018 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,941 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,291 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,657 questions
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 25,956 Reputation points
    2022-08-17T12:58:16.167+00:00

    Hi @Shambhu Rai ,

    Please try the following solution. It will work starting from SQL Server 2017 onwards.

    SQL

    -- DDL and sample data population, start  
    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, details VARCHAR(100));  
    INSERT INTO @tbl values   
    ('20kjk'),  
    ('3ddf'),  
    ('561.456/.7890');  
    -- DDL and sample data population, end  
      
    -- SQL Server 2017 onwards  
    ;WITH rs AS   
    (  
       SELECT *  
          , REPLACE(TRANSLATE(details, '0123456789', SPACE(10)),' ','') AS JunkCharacters  
       FROM @tbl  
    )  
    SELECT rs.*  
       , REPLACE(TRANSLATE(details, TRIM(JunkCharacters), SPACE(LEN(TRIM(JunkCharacters)))),' ','') AS CleansedDetails  
    FROM rs;  
    

    Output

    +----+---------------+----------------+-----------------+  
    | ID |    details    | JunkCharacters | CleansedDetails |  
    +----+---------------+----------------+-----------------+  
    |  1 | 20kjk         | kjk            |              20 |  
    |  2 | 3ddf          | ddf            |               3 |  
    |  3 | 561.456/.7890 | ./.            |      5614567890 |  
    +----+---------------+----------------+-----------------+  
    
    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anurag Sharma 17,606 Reputation points
    2022-08-17T11:49:42.32+00:00

    Hi @Shambhu Rai , welcome to Microsoft Q&A platform.

    If the number always comes at the start, then you can use below:

    select LEFT('3ddf', PATINDEX('%[0-9][^0-9]%', '3ddf'))  
    

    You can also use the same in insert statement before inserting it to database:

    insert into test1  
        values(LEFT('20kjk', PATINDEX('%[0-9][^0-9]%', '20kjk' )) ,LEFT('3ddf', PATINDEX('%[0-9][^0-9]%', '3ddf' )))  
    

    Please let me know if this helps or else we can discuss further.

    Reference: split alpha and numeric using sql


  2. Alberto Morillo 34,056 Reputation points MVP
    2022-08-17T11:59:39.687+00:00

    Create this function:

    CREATE Function [fnRemoveNonNumericCharacters](@strText VARCHAR(1000))  
    RETURNS VARCHAR(1000)  
    AS  
    BEGIN  
        WHILE PATINDEX('%[^0-9]%', @strText) > 0  
        BEGIN  
            SET @strText = STUFF(@strText, PATINDEX('%[^0-9]%', @strText), 1, '')  
        END  
        RETURN @strText  
    END  
    

    then you can use it as shown below:

    CREATE TABLE test1  
    (id varchar(100), details varchar(100))  
      
    INSERT INTO test1  
    ([Id], [details])  
    SELECT dbo.fnRemoveNonNumericCharacters('20kjk'), dbo.fnRemoveNonNumericCharacters('3ddf')  
    
    0 comments No comments

  3. Isabellaz-1451 3,616 Reputation points
    2022-08-18T06:07:17.663+00:00

    Hi

    Please check a look at this thread,hope will help you.
    https://sqlzealots.com/2017/05/28/how-to-extract-only-numbers-from-a-string-in-sql-server/

    Best Regards,
    Isabella

    0 comments No comments

  4. LiHongMSFT-4306 28,271 Reputation points
    2022-08-18T08:53:16.023+00:00

    Hi @Shambhu Rai
    Have you tried the TRANSLATE function as Yitzhak answered? If your SQL Sever doesn't support this function, then you could have a try on looped REPLACE.
    Check this:

    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, details VARCHAR(100));  
    INSERT INTO @tbl values   
     ('20kjk'),  
     ('3ddf'),  
     ('ER78Y9'),  
     ('561.456/.7890');  
          
    SELECT *,   
           REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(  
    	   details,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9','')AS New_Value  
    FROM @tbl  
    

    Best regards,
    LiHong

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.