Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL new lines following Export/Import procedure

Posted on 2011-03-18
5
257 Views
Last Modified: 2012-05-11
Dear All,
    I'm in the process of moving one database into a new system and I'm encountering the following issue.

In the original database I have a field Message which is an ntext the line spacings are visible when hovering over the field even though the field looks as as a single line. When importing to the new database and into a different system I need to replace the new line identifier with </p><p> and place an <p> in front of the message and a </p> at the end thus if the message looks like this

Helllo, this is the first line and this is the second.

it should be converted to

<p>Hello,</p><p> this is the first line</p><p>and this is the second</p>

any ideas ?

regards
 Original Database view
0
Comment
Question by:Biju708
  • 4
5 Comments
 
LVL 6

Accepted Solution

by:
dan_mason earned 500 total points
ID: 35164749
Yes, what you need to do is find out exactly what characters are coming in that are causing that line break. When I was facing a similar problem I put together a script that will show you the ASCII values for every character in a string. If you run it against the field in question you'll be able to see where the carriage returns are happening, and the characters involved (it can vary, depending on the original source).

Run the script below to set up the function, then you would execute it like this:

DECLARE @input varchar(max)
SELECT @input=YourField FROM YourTable

SELECT * FROM dbo.tbCharToAscii(@input)

The result will probably be that for a new line you see the figures 13 and 10, in that order, but it might just be one of them or something else.

Anyway, assuming it was 13 followed by 10, you'd then manipulate your string like this:

SET YourField = '<p>'+REPLACE(YourField,CHAR(13)+CHAR(10),'</p><p>')+'</p>'






CREATE FUNCTION [dbo].[tbCharToAscii](@value nvarchar(max))
RETURNS @oT TABLE (pos int PRIMARY KEY,character nchar(1),code varchar(4))
AS
BEGIN

DECLARE @n smallint
SELECT @n=DATALENGTH(@value)/2

WHILE @n>0
	BEGIN
		INSERT @oT (pos,Character,Code)
		SELECT @n,SUBSTRING(@value,@n,1), ASCII(SUBSTRING(@value,@n,1))
		SELECT @n=@n-1
	END

RETURN
END

Open in new window

0
 

Author Comment

by:Biju708
ID: 35164957
Thanks I have some errors on your script though. Can you please have a look maybe I have something wrong.
CREATE FUNCTION [dbo].[tbCharToAscii](@value nvarchar(max))
RETURNS @oT TABLE (pos int PRIMARY KEY,character nchar(1),code varchar(4))
AS
BEGIN

DECLARE @n smallint
SELECT @n=DATALENGTH(@value)/2

WHILE @n>0
	BEGIN
		INSERT @oT (pos,Character,Code)
		SELECT @n,SUBSTRING(@value,@n,1), ASCII(SUBSTRING(@value,@n,1))
		SELECT @n=@n-1
	END

RETURN
END

DECLARE @input varchar(max)
SELECT @input=R_MESSAGE FROM [Database001].[dbo].[PORTAL_REPLY]
SELECT * FROM dbo.tbCharToAscii(@input)

Open in new window

0
 

Author Comment

by:Biju708
ID: 35164963
with errors::

Msg 156, Level 15, State 1, Procedure tbCharToAscii, Line 19
Incorrect syntax near the keyword 'DECLARE'.
0
 

Author Comment

by:Biju708
ID: 35232561
Dear Dan,
   thanks for your input I have worked a little bit on your script and identified where the problem was it's now working like a charm for me it was just a question of only using Char(10) and not CHAR(13)+CHAR(10)

thanks
0
 

Author Closing Comment

by:Biju708
ID: 35232571
I just needed to change to Char(10) only
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question