0

I have a columns in tbl in which i have values like

X Y Z 1 4 123/1232221 2 3 234/3454455 3 2 UNKNOWN 123234 

Now what i want is when i trigger a query 'UNKNOWN' will replace with space

Like below:

X Y Z 1 4 123/1232221 2 3 234/3454455 3 2 123234 

Any help will be appreciated.

Thanks In advance !!!

3
  • 2
    what about replace(z,'UNKNOWN','') ? Commented May 12, 2017 at 12:49
  • I put your code waiting for output as it is very large database it will take 2 hours to run it completly Commented May 12, 2017 at 13:15
  • Thanks @Ganesh_Devlekar your code work for me Commented May 15, 2017 at 4:47

3 Answers 3

2

To correct the table :

update tbl set z = replace(z,'UNKNOWN','') where z like '%UNKNOWN%'; 

To query the table with the UNKNOWN replaced

select x, y, replace(z,'UNKNOWN','') as z from tbl; 
Sign up to request clarification or add additional context in comments.

1 Comment

Uhm, so is that good or bad news? If you ran that first update statement then yeah, there shouldn't be any "UNKNOWN" in that z field in the table.
1

Would this suffice? UPDATE table SET column_name = REPLACE(column_name, 'UNKNOWN', ' ');

Comments

0

use replace function, REPLACE ( string_expression , string_pattern , string_replacement ) in your case string expression in value of your column z. If in some row column doesn't have value with string 'UNKNOWN', value of that column in row will remain the same. you can find more on https://learn.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql

select x,y,replace(z,'UNKNOWN',' ') as z from you_table 

or if you want to update

update your_table set z=replace(z,'UNKNOWN',' ') 

1 Comment

From review queue: May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.