Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Management Studio 2012 - replacing carriage returns

Posted on 2013-12-19
3
Medium Priority
?
1,955 Views
Last Modified: 2013-12-23
I'm running into the issue with a SQL user where data copied from the SQL Management Studio 2012 query results tab and then pasted into Excel results in a bunch of carriage return lines and messes up the paste in Excel.

The "Comments" column in particular has this problem with carriage returns.

Where should the statement in SQL go to get rid of the carriage returns to result in a single-line of text paste to Excel when copied from SQL Management Studio query Results tab?

select 
AssetID,
Category,
EnteredBy,
REPLACE(Text,'"',' ') As Comment,
REPLACE(text, CHAR(13) + CHAR(10), ', '),

Number As CommentNumber,
Max(Date) As CommentDate,
de.NAME_SLS_SPCLST,
de.NAME_SLS_SPCLST_MGR,
de.NAME_RPR_SPCLST,
de.STGY_VAL,
DE.DII,
DE.FLAG_CMPLX_TTL_ACTIVE,
DE.DT_FST_STR_APPRD
--COUNT(Text) As DubCheckByCommentDate
From client.hsc.Comments as C
Left Join
ckuebt.dbo.ImportData_LastImport de
--client.dbo.Inventory de

ON
c.AssetID = de.ID_AST
Where 
--Importid = 8224 AND
Category like 'Iam%'
AND
c.Date = 
       (Select 
              MAX(Date)
       From client.hsc.Comments
       Where Category like 'Iam%'
                     and 
                AssetID = c.AssetID
       Group By AssetID
       )

              --and
              --AssetID = 662438
              and
              Number = (Select
MAX(Number)from client.hsc.Comments
                     where Category like 'Iam%'
                     
                     and AssetID = c.AssetID
                     group by AssetID
                     
              )
              and
              AssetID IN (
675206,
692788,
710061,
779368,
1102107


                                  )

                     
Group By AssetID, Category, EnteredBy, text,Number,de.NAME_SLS_SPCLST,
de.NAME_SLS_SPCLST_MGR,
de.NAME_RPR_SPCLST,
de.STGY_VAL,
DE.DII,
DE.FLAG_CMPLX_TTL_ACTIVE,
DE.DT_FST_STR_APPRD



Order By AssetID

 
----------------------second filter pull-----------------------------------

 

Open in new window

0
Comment
Question by:garryshape
3 Comments
 
LVL 9

Assisted Solution

by:QuinnDex
QuinnDex earned 400 total points
ID: 39731352
sometimes you dont find both char(13) and Char(10) this will stop the replace from finding a match.

try the following if that doesnt work change it to char(13)

select 
AssetID,
Category,
EnteredBy,
REPLACE(Text,'"',' ') As Comment,
REPLACE(text, CHAR(10), ', '),

Number As CommentNumber,
Max(Date) As CommentDate,
de.NAME_SLS_SPCLST,
de.NAME_SLS_SPCLST_MGR,
de.NAME_RPR_SPCLST,
de.STGY_VAL,
DE.DII,
DE.FLAG_CMPLX_TTL_ACTIVE,
DE.DT_FST_STR_APPRD
--COUNT(Text) As DubCheckByCommentDate
From client.hsc.Comments as C
Left Join
ckuebt.dbo.ImportData_LastImport de
--client.dbo.Inventory de

ON
c.AssetID = de.ID_AST
Where 
--Importid = 8224 AND
Category like 'Iam%'
AND
c.Date = 
       (Select 
              MAX(Date)
       From client.hsc.Comments
       Where Category like 'Iam%'
                     and 
                AssetID = c.AssetID
       Group By AssetID
       )

              --and
              --AssetID = 662438
              and
              Number = (Select
MAX(Number)from client.hsc.Comments
                     where Category like 'Iam%'
                     
                     and AssetID = c.AssetID
                     group by AssetID
                     
              )
              and
              AssetID IN (
675206,
692788,
710061,
779368,
1102107


                                  )

                     
Group By AssetID, Category, EnteredBy, text,Number,de.NAME_SLS_SPCLST,
de.NAME_SLS_SPCLST_MGR,
de.NAME_RPR_SPCLST,
de.STGY_VAL,
DE.DII,
DE.FLAG_CMPLX_TTL_ACTIVE,
DE.DT_FST_STR_APPRD



Order By AssetID

 
----------------------second filter pull-----------------------------------

 
                                  

Open in new window

0
 
LVL 35

Accepted Solution

by:
James0628 earned 1600 total points
ID: 39733486
QuinnDex has a point, but the other thing that I noticed is that you have two separate REPLACE functions, in separate columns.

 You have

REPLACE(Text,'"',' ') As Comment,

 which creates a column named Comment.

 But then you have the CR-LF replace in a separate, unnamed, column:

REPLACE(text, CHAR(13) + CHAR(10), ', '),


 If you want your Comment column to be the result from both REPLACE functions, you can nest them:

REPLACE(REPLACE(Text,'"',' '), CHAR(13) + CHAR(10), ', ') As Comment,


 James
0
 

Author Comment

by:garryshape
ID: 39735262
Thanks guys I haven't tried yet but will within the next 1 - 2 days and report back.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

824 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