What will be the output of

@str AS VARCHAR(500)
,@xml AS XML
,@delimiter AS VARCHAR(1)

--Set the original string
SET @str= 'Hello!!!This is text1 (text1).This is text2(text2).This is text3(text3) and text4(text4)'

--Set the delimiter
SET @delimiter ='@'

-- Replace the ( and ) with '@-' and '-@' respectively
SELECT @str =REPLACE(REPLACE(@str,'(','@-'),')','-@')

-- Replace the '@' symbols with </X><X>
SET @xml = CAST(('<X>'+REPLACE(@str,@delimiter ,'</X><X>')+'</X>') AS XML)

--Query the XML using XQuery
N.value('.', 'varchar(500)') AS QueryResult
FROM @xml.nodes('X') AS T(N)
WHERE PATINDEX('%[-]%', N.value('.', 'varchar(100)')) = 0
AND LEN(N.value('.', 'varchar(500)')) > 0)

SELECT ExtractedText =

 Posted by Rajnilari2015 on 1/24/2016 | Category: Sql Server Interview questions | Views: 330 | Points: 40
Select from following answers:
  1. Hello!!!This is text1 .This is text2.This is text3 and text4
  2. Hello!!!This is text1 (text1).This is text2(text2).This is text3(text3) and text4(text4)
  3. Hello!!!This is (text1).This is (text2).This is (text3) and (text4)
  4. None of the above
  5. All Above

Show Correct Answer

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response