-1

I have values in my column as below. Can anyone help me with how to replace any numeric data present in a column or string to blank using SQL Server query?

enter image description here

Below is the column data. How do I replace the numbers to blank and display only underscores.

3
  • 6
    As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. Commented May 25, 2021 at 7:08
  • use translate() Commented May 25, 2021 at 7:14
  • to separate numeric part and do any operation on it, check this link it might help : stackoverflow.com/q/10443462/6876710 Commented May 25, 2021 at 7:16

2 Answers 2

1

You could approach this by counting the number of underscores, and then generating a string containing this number of underscores:

SELECT Column1, REPLICATE('_', LEN(Column1) - LEN(REPLACE(Column1, '_', '')))
FROM yourTable;

screen capture from demo link below

Demo

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a more generic solution. It will handle not just the underscore chars. It will work starting from SQL Server 2017 onwards.

As @Squirrel correctly mentioned, the TRANSLATE() function is very handy for such cases.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, col VARCHAR(256));
INSERT INTO @tbl (col) VALUES
('2413347_6752318'),
('7263_872_767'),
('123Code456');
-- DDL and sample data population, end

SELECT col AS [Before] 
    , REPLACE(TRANSLATE(col, '0123456789', SPACE(10)), SPACE(1), '') AS [After]
FROM @tbl;

Output

+-----------------+-------+
|     Before      | After |
+-----------------+-------+
| 2413347_6752318 | _     |
| 7263_872_767    | __    |
| 123Code456      | Code  |
+-----------------+-------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.