Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
I am using SQL server 2008
I have a string with delimiter full stop(.).

I want to separate string in the next line to display in the Crystal report


string is like

str ='aaaaaaaaaaaaaa.bbbbbbbbbbbbb.cccccccccccccc.ddddddddddddddd'

result
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
ccccccccccccccc
ddddddddddddddd

Pls help

regards
Nirmala

What I have tried:

DECLARE @strInput nVARCHAR(Max), @strResult nVARCHAR(max);
SET @strInput = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,cccccccccccccccccccccccccccccc.ddddddddddddddddddddddd.eeeeeeeeeeeeeeeeeeeeeeeeeeeee';
SET @strResult = REPLACE(@strInput, ',',CHAR(13));
SELECT @strResult AS 'String with Line Feed';
GO
Posted
Updated 29-Dec-21 7:26am

Depending on where you want to use the text the newline may vary. In Windows the newline is using two characters; Carriage return and line feed. In order to replace dots with cr+lf have a try with
SQL
DECLARE @strInput nVARCHAR(Max), @strResult nVARCHAR(max);
SET @strInput = 'aaaaaaaaaaaaaa.bbbbbbbbbbbbb.cccccccccccccc.ddddddddddddddd';
SET @strResult = REPLACE(@strInput, '.', char(13) + char(10));
SELECT @strResult AS 'String with Line Feed';
GO

Also note that in your code example you have both dots and commas so you may have to do two separate replace calls, depending on the requirements.
 
Share this answer
 
Comments
Nirmala Saravanan 29-Dec-21 1:37am    
i have tried this also. but not working
Wendelius 29-Dec-21 1:55am    
Can you elaborate? Just tried running it in SSMS and works the new lines are correctly in place.
Wendelius 29-Dec-21 2:17am    
A quick check, in the Crystal report, do you have Can grow property enabled for the object?
In a newer version of SQL Server you can use STRING_SPLIT (Transact-SQL) - SQL Server | Microsoft Docs[^].

For older version of SQL Server, you need to create custom function (for example: Converting comma separated data in a column to rows for selection[^] ) or write query, which uses CTE[^].
See:
SQL
CREATE TABLE StupidData
(
   LongString NVARCHAR(MAX)
)

INSERT INTO StupidData (LongString)
VALUES(N'aaaaaaaaaaaaaa.bbbbbbbbbbbbb.cccccccccccccc.ddddddddddddddd');

;WITH CTE AS
(
  SELECT 1 AS Level, LEFT(LongString, CHARINDEX('.', LongString)-1) AS WordPart, 
    RIGHT(LongString, LEN(LongString)- CHARINDEX('.', LongString)) AS Remainder
  FROM StupidData
  WHERE CHARINDEX('.', LongString)>0
  UNION ALL
  SELECT Level + 1 AS Level, LEFT(Remainder, CHARINDEX('.', Remainder)-1) AS WordPart, 
    RIGHT(Remainder, LEN(Remainder)- CHARINDEX('.', Remainder)) AS Remainder
  FROM CTE
  WHERE CHARINDEX('.', Remainder)>0
  UNION ALL
  SELECT Level + 1 AS Level, Remainder AS WordPart, NULL AS Remainder
  FROM CTE
  WHERE CHARINDEX('.', Remainder)=0
)
SELECT *
FROM CTE


Returns:
Level 	WordPart 	Remainder
1 	aaaaaaaaaaaaaa 	bbbbbbbbbbbbb.cccccccccccccc.ddddddddddddddd
2 	bbbbbbbbbbbbb 	cccccccccccccc.ddddddddddddddd
3 	cccccccccccccc 	ddddddddddddddd
4 	ddddddddddddddd


Online example: SQL Server 2019 | db<>fiddle[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900