Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace data in a column for a character in SQL

I have a table having data like as below:

  Description   Name
    ABC           AB
    ABCD          AB, BC, CD
    ABCDF         AB, BC

Now i needed output as below:

  Description   Name
    ABC           AB
    ABCD          AB, BC and CD
    ABCDF         AB and BC

How can i get desired output in SQL? please help me out.

like image 806
Aryan Avatar asked Jun 12 '26 11:06

Aryan


2 Answers

Please try:

select 
    Description, 
    ISNULL(
       REVERSE(STUFF(REVERSE(Name), CHARINDEX(',', REVERSE(Name), 0),1,'dna ')), 
    Name) Name
From YourTable

SQL Fiddle Demo

like image 169
TechDo Avatar answered Jun 13 '26 23:06

TechDo


Do it as

declare @str nvarchar(200)
set @str = 'Ali, ahmed, riaz, zoya'
select SUBSTRING(@str, 0, (len(@str) - charindex(',', reverse(@str)))) +
Replace(SUBSTRING(@str, (len(@str) - charindex(',', reverse(@str))), len(@str)), 
', ', ' and ')
like image 41
Lali Avatar answered Jun 14 '26 01:06

Lali



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!