3

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.

2 Answers 2

1

Please try:

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

SQL Fiddle Demo

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

Comments

1

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 ')

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.