Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a specific text from the query result

Tags:

sql

sql-server

My query:

select SeqNo, Name, Qty, Price 
from vendor 
where seqNo = 1;

outputs like below:

SeqNo   Name    Qty  Price
1       ABC     10   11
1       -do-    11   12
1       ditto   13   14

The output above shows the vendor name as ABC in first row which is correct. Later on as users entered for the same vendor name "ABC" as either '-do-' / 'ditto'. Now in my final query output I want to replace -do- and ditto with ABC (as in above example) so my final output should look like:

SeqNo   Name    Qty  Price
1       ABC     10   11
1       ABC     11   12
1       ABC     13   14
like image 384
Programmer Avatar asked Dec 07 '25 02:12

Programmer


1 Answers

this is working in sql server for you sample data..not sure how your other rows are look like

select SeqNo,
       case when Name in ('-do-','ditto') then 
        (select Name from test where Name not in('-do-','ditto')
        and SeqNo = 1)
        else Name
        end as Name
from table
where SeqNo = 1
like image 77
Sachu Avatar answered Dec 08 '25 15:12

Sachu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!