remove values from string

Shambhu Rai 1,406 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.
13,215 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,855 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,258 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,592 questions
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 25,721 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. Ebrahim Bazarjani 5 Reputation points
    2024-05-11T10:29:32.2366667+00:00

    Hi,

    You can Use This :

    CREATE FUNCTION udfFG_GetNumFromStr (@Str VARCHAR(64))
    RETURNS VARCHAR(64)
    AS
    BEGIN
    	DECLARE @Pos int,@Char char(1)
    	SET @Pos = PATINDEX('%[^0-9]%',@Str)
    	WHILE @Pos > 0
    	BEGIN
    		SET @Char = SUBSTRING(@Str,@Pos,1)
    		SET @Str = REPLACE(@Str,@Char,'')
    		SET @Pos = PATINDEX('%[^0-9]%',@Str)
    	END
    RETURN @Str
    END
    
    0 comments No comments