Saturday, April 26, 2014

New line for Text in a row

Bhasker Alle, Using REPLACE the output will not come the way you have desired. It'll come in single row. But when you'll copy and paste the o/p as text it'll definitely be in NewLines(CR).


If you need the output in SSMS as well then below is the sample code. Check this.



DECLARE @S VARCHAR(4000)='MICROSOFT\APPLE\GOOGLE\YAHOO\XEROX\INTEL'

DECLARE @Table TABLE(ID INT IDENTITY,String VARCHAR(1000))
DECLARE @Run INT = 1
DECLARE @Count INT
SET @Count=(SELECT LEN(@S)-LEN(REPLACE(@S,'\','')))

WHILE @Run<=@Count
BEGIN
INSERT @Table(String)
SELECT LEFT(@S,CHARINDEX('\',@S)-1)
SET @S=RIGHT(@S,LEN(@S)-CHARINDEX('\',@S))
SET @Run=@Run+1
END
INSERT @Table(String)
SELECT @S
SELECT * FROM @Table





Chaos isn’t a pit. Chaos is a ladder. Many who try to climb it fail and never get to try again. The fall breaks them. And some are given a chance to climb, but they refuse. They cling to the realm, or the gods, or love. Illusions. Only the ladder is real. The climb is all there is.



No comments:

Post a Comment