-4

Input:

Col1 Col2
DBA Stored Procedure
DBA Functions
DBA Triggers

Output:

Col1 Col2
DBA Stored Procedure, Functions, Triggers
HARISH VM
  • 1
  • 3
  • 2
    It's not even clear how you're supposed to get from input to output. Why does `DBA` disappear for these two rows? It looks like you're trying to manipulate a result set for display purposes, which is something much better done in client code than in the query itself. – Jeroen Mostert Jul 28 '22 at 06:10

1 Answers1

0
select
     case when substring(Col2,2,1)='u' then 'DBA' else '' end as Col1
    ,Col2
from yourtable
George Menoutis
  • 6,894
  • 3
  • 19
  • 43
  • 1
    I stand my ground on this answer for entertainment purposes – George Menoutis Jul 28 '22 at 06:27
  • It's correct but it works on a trick which you have taken u on Col2 using case statement what happened if two or more rows having U it can print there also right so that we have to write a query dynamically – HARISH VM Jul 28 '22 at 06:50
  • 1
    If there are more rows, you should tell us what you are trying to do. I used "u" as a funnily arbitrary example in order to indicate how blind we are trying to help you. – George Menoutis Jul 28 '22 at 07:10
  • @HARISHVM There is no `case` _statement_, but there is a [`case`](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-ver15) _expression_. And the answer isn't complete without `order by Substring( Col2, 3, 1 ) desc;`. If you have a better explanation of your requirements than "Input"/"Output" then we might be able to offer a more suitable answer. Please read [this](https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) for some tips on improving your question. – HABO Jul 28 '22 at 12:59
  • @HABO Isn't it contradictory to assume the explanation is not clear enough and also correct my answer? – George Menoutis Jul 28 '22 at 15:55
  • I was not suggesting that _you_ change your answer. Instead I addressed the OP to point out that their detailed problem statement made no mention of the order of the results and that if one were to _assume_ that the "Output" was in the desired order then an `order by` clause was needed. The one I suggested is engineered as precisely as your `case` to arrive at the "Output" from the "Input" with no consideration of additional input data. – HABO Jul 28 '22 at 16:12
  • @HABO fair enough – George Menoutis Jul 28 '22 at 16:47
  • Hi now I edited the question hope you understand the question.... – HARISH VM Aug 01 '22 at 06:09
  • Search for STRING_AGG or, if on version <2016, XML PATH concatenation – George Menoutis Aug 01 '22 at 07:23