Solved

SQL comma

Posted on 2014-11-18
10
253 Views
Last Modified: 2014-11-18
Hello,
I use below script to export data, (and save to csv through an exporter program,
the Reference field has commas in it,
when I export , the fields with commas become separated, , I need to keep the field together.  
Example  Reference :  100sd675,PMExvtyr

      SELECT
          CONVERT(varchar,InvoiceNumber) AS [InvoiceNumber],
            CONVERT(Varchar,Reference) as [Reference]
      FROM FinalizedOrders
      WHERE  ....
0
Comment
Question by:W.E.B
10 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40451147
easiest option is to make a different character instead of a comma for separating the fields say !

     SELECT
           CONVERT(varchar,InvoiceNumber) AS [InvoiceNumber],
            '!'+ CONVERT(Varchar,Reference) as [Reference]
       FROM FinalizedOrders
       WHERE  ....
0
 

Author Comment

by:W.E.B
ID: 40451152
I can't,
this is a client entry.

I found my own solution though,
'"' + Cast(Reference as varchar(100)) + '"' AS [Reference] ,

thanks
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40451156
I'm afraid the above is correct:  If you have a comma-separated text file, then the values in it cannot have commas in it, as it will cause the ETL process that consumes that file to interpret the row as having more columns than is currently mapped, and fail.

Your choices:
Change the delimeter to something else, such as pipe (|) delimited file, as pipes are hardly every used in values, except for columns that contain email text where some people's signature blocks have pipes in them, such as Dave Swift  | Super Dude | Green Co.
Use the REPLACE function to either remove the commas, or replace them with some other value...
SELECT
   REPLACE(CONVERT(varchar,InvoiceNumber), ',', '') AS [InvoiceNumber],
   REPLACE(CONVERT(Varchar,Reference), ',', '')  as [Reference]
FROM FinalizedOrders
WHERE  ....

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:W.E.B
ID: 40451236
I've requested that this question be deleted for the following reason:

Found own sloution
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40451237
Objection.  Spell out what 'Found own solutions' means here, so we can accept that as the answer, so other people with similar issues can learn from this question.
0
 

Author Comment

by:W.E.B
ID: 40451254
SELECT
          CONVERT(varchar,InvoiceNumber) AS [InvoiceNumber],
'"' + Cast(Reference as varchar(100)) + '"' AS [Reference]
       FROM FinalizedOrders
       WHERE  ....
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40451265
Objection? Jim he spelled out what his solution was already!  Enclose the string in "     ". Normal for any CSV that has commas in it.
0
 

Author Comment

by:W.E.B
ID: 40451342
So , my own solution (Comment before Jim Comment)
'"' + Cast(Reference as varchar(100)) + '"' AS [Reference]

Is the same as Jim?

Jim says,
Your choices:
    Change the delimeter to something else, such as pipe (|) delimited file, as pipes are hardly every used in values, except for columns that contain email text where some people's signature blocks have pipes in them, such as Dave Swift  | Super Dude | Green Co.

Use the REPLACE function to either remove the commas, or replace them with some other value...

?
0
 

Author Comment

by:W.E.B
ID: 40451361
I will award the points to Jim, for his effort and time.

Thank you Jim.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40451445
We must have cross-posted the last comment, and my objection to the close request.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now