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.
Please try:
select
Description,
ISNULL(
REVERSE(STUFF(REVERSE(Name), CHARINDEX(',', REVERSE(Name), 0),1,'dna ')),
Name) Name
From YourTable
SQL Fiddle Demo
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 ')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With